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 
15 class SubProblem;
16 class SystemBase;
17 namespace libMesh
18 {
19 class Elem;
20 }
21 
22 namespace Moose
23 {
27 inline bool
29 {
30  return true;
31 }
32 
77 inline std::size_t
78 adOffset(unsigned int var_num,
79  std::size_t max_dofs_per_elem,
80  ElementType element_type = ElementType::Element,
81  unsigned int num_vars_in_system = 0)
82 {
83  // If our element type is anything other than ElementType::Element, then the user must
84  // supply num_vars_in_system in order to calculate the offset
85  mooseAssert(element_type == ElementType::Element || num_vars_in_system,
86  "If our element type is anything other than ElementType::Element, then you "
87  "must supply num_vars_in_system in order to calculate the offset");
88 
89  switch (element_type)
90  {
92  return var_num * max_dofs_per_elem;
93 
95  return num_vars_in_system * max_dofs_per_elem + var_num * max_dofs_per_elem;
96 
97  case ElementType::Lower:
98  return 2 * num_vars_in_system * max_dofs_per_elem + var_num * max_dofs_per_elem;
99 
100  default:
101  mooseError(
102  "Unsupported element type ",
103  static_cast<typename std::underlying_type<decltype(element_type)>::type>(element_type));
104  }
105 }
106 
107 inline std::size_t
108 adOffset(unsigned int var_num,
109  std::size_t max_dofs_per_elem,
110  DGJacobianType dg_jacobian_type,
111  unsigned int num_vars_in_system = 0)
112 {
113  if (dg_jacobian_type == DGJacobianType::ElementElement ||
114  dg_jacobian_type == DGJacobianType::NeighborElement)
115  return adOffset(var_num, max_dofs_per_elem, ElementType::Element);
116  else
117  return adOffset(var_num, max_dofs_per_elem, ElementType::Neighbor, num_vars_in_system);
118 }
119 
123 std::unordered_map<dof_id_type, Real>
124 globalDofIndexToDerivative(const ADReal & ad_real,
125  const SystemBase & sys,
126  ElementType elem_type = ElementType::Element,
127  THREAD_ID tid = 0);
128 
133 template <typename T>
134 auto
135 globalDofIndexToDerivative(const T & ad_real_container,
136  const SystemBase & sys,
137  ElementType elem_type = ElementType::Element,
138  THREAD_ID tid = 0)
139  -> std::vector<std::unordered_map<
140  dof_id_type,
141  typename std::enable_if<std::is_same<ADReal, typename T::value_type>::value, Real>::type>>
142 {
143  std::vector<std::unordered_map<dof_id_type, Real>> ret_val(ad_real_container.size());
144 
145  for (MooseIndex(ad_real_container) i = 0; i < ad_real_container.size(); ++i)
146  ret_val[i] = globalDofIndexToDerivative(ad_real_container[i], sys, elem_type, tid);
147 
148  return ret_val;
149 }
150 
154 bool doDerivatives(const SubProblem & subproblem, const SystemBase & sys);
155 }
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:28
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:46
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:78
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