LCOV - code coverage report
Current view: top level - src/utils - ADUtils.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 2 29 6.9 %
Date: 2025-07-17 01:28:37 Functions: 1 3 33.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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>
      27           0 : globalDofIndexToDerivative(const ADReal & ad_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           0 :   const Assembly & assembly = sys.subproblem().assembly(tid, sys.number());
      35             :   const Elem * elem;
      36           0 :   switch (elem_type)
      37             :   {
      38           0 :     case ElementType::Element:
      39           0 :       elem = assembly.elem();
      40           0 :       break;
      41             : 
      42           0 :     case ElementType::Neighbor:
      43           0 :       elem = assembly.neighbor();
      44           0 :       break;
      45             : 
      46           0 :     case ElementType::Lower:
      47           0 :       elem = assembly.lowerDElem();
      48           0 :       break;
      49             : 
      50           0 :     default:
      51           0 :       mooseError("Unrecognized element type");
      52             :   }
      53             : 
      54           0 :   std::unordered_map<dof_id_type, Real> ret_val;
      55             : 
      56           0 :   const System & libmesh_sys = sys.system();
      57           0 :   const DofMap & dof_map = libmesh_sys.get_dof_map();
      58             : 
      59           0 :   const unsigned int num_vars = libmesh_sys.n_vars();
      60             : 
      61           0 :   const auto max_dofs_per_elem = sys.getMaxVarNDofsPerElem();
      62             : 
      63           0 :   for (unsigned int var_num = 0; var_num < num_vars; ++var_num)
      64             :   {
      65           0 :     std::vector<dof_id_type> global_indices;
      66             : 
      67             :     // Get the global indices corresponding to var_num that exist on elem
      68           0 :     dof_map.dof_indices(elem, global_indices, var_num);
      69             : 
      70             :     // determine the AD offset for the current var
      71           0 :     const auto ad_offset = adOffset(var_num, max_dofs_per_elem, elem_type, num_vars);
      72             : 
      73             :     // Map from global index to derivative
      74           0 :     for (MooseIndex(global_indices) local_index = 0; local_index < global_indices.size();
      75             :          ++local_index)
      76           0 :       ret_val[global_indices[local_index]] = ad_real.derivatives()[ad_offset + local_index];
      77           0 :   }
      78             : 
      79           0 :   return ret_val;
      80             : }
      81             : 
      82             : bool
      83    26175419 : doDerivatives(const SubProblem & subproblem, const SystemBase & sys)
      84             : {
      85    26175419 :   return ADReal::do_derivatives && sys.number() == subproblem.currentNlSysNum();
      86             : }
      87             : }

Generated by: LCOV version 1.14