https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
23namespace Moose
24{
25
26std::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
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
82bool
83doDerivatives(const SubProblem & subproblem, const SystemBase & sys)
84{
85 return ADReal::do_derivatives && sys.number() == subproblem.currentNlSysNum();
86}
87}
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
unsigned int THREAD_ID
Definition MooseTypes.h:237
Keeps track of stuff related to assembling.
Definition Assembly.h:110
const Elem *const & elem() const
Return the current element.
Definition Assembly.h:414
const Elem *const & neighbor() const
Return the neighbor element.
Definition Assembly.h:470
const Elem *const & lowerDElem() const
Return the lower dimensional element.
Definition Assembly.h:476
Nonlinear system to be solved.
Generic class for solving transient nonlinear problems.
Definition SubProblem.h:79
virtual unsigned int currentNlSysNum() const =0
virtual Assembly & assembly(const THREAD_ID tid, const unsigned int sys_num)=0
Base class for a system (of equations)
Definition SystemBase.h:87
unsigned int number() const
Gets the number of this system.
SubProblem & subproblem()
Definition SystemBase.h:102
std::size_t getMaxVarNDofsPerElem() const
Gets the maximum number of dofs used by any one variable on any one element.
Definition SystemBase.h:604
virtual libMesh::System & system()=0
Get the reference to the libMesh system.
unsigned int n_vars() const
const DofMap & get_dof_map() const
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
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