https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
15class SubProblem;
16class SystemBase;
17namespace libMesh
18{
19class Elem;
20}
21
22namespace Moose
23{
27inline bool
29{
30 return true;
31}
32
77inline std::size_t
78adOffset(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
98 return 2 * num_vars_in_system * max_dofs_per_elem + var_num * max_dofs_per_elem;
99
100 default:
102 "Unsupported element type ",
103 static_cast<typename std::underlying_type<decltype(element_type)>::type>(element_type));
104 }
105}
106
107inline std::size_t
108adOffset(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
123std::unordered_map<dof_id_type, Real>
124globalDofIndexToDerivative(const ADReal & ad_real,
125 const SystemBase & sys,
127 THREAD_ID tid = 0);
128
133template <typename T>
134auto
135globalDofIndexToDerivative(const T & ad_real_container,
136 const SystemBase & sys,
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
154bool doDerivatives(const SubProblem & subproblem, const SystemBase & sys);
155
159template <bool is_ad>
162{
163 if constexpr (is_ad)
164 return u_ad;
165 else
166 return u_ad.value();
167}
168}
DualNumber< Real, DNDerivativeType, true > ADReal
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Moose::GenericType< Real, is_ad > GenericReal
Definition MooseTypes.h:698
unsigned int THREAD_ID
Definition MooseTypes.h:237
Generic class for solving transient nonlinear problems.
Definition SubProblem.h:79
Base class for a system (of equations)
Definition SystemBase.h:87
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
GenericReal< is_ad > ADRealToGenericReal(const ADReal &u_ad)
Converts an ADReal to a GenericReal<is_ad>
Definition ADUtils.h:161
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
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
bool globalADIndexing()
Whether we are using global AD indexing.
Definition ADUtils.h:28
DGJacobianType
Definition MooseTypes.h:804
@ ElementElement
Definition MooseTypes.h:805
@ NeighborElement
Definition MooseTypes.h:807
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...