LCOV - code coverage report
Current view: top level - src/userobjects - GhostingUserObject.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 48 51 94.1 %
Date: 2025-08-08 20:01:16 Functions: 5 5 100.0 %
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 "GhostingUserObject.h"
      11             : #include "NonlinearSystemBase.h"
      12             : #include "MooseMesh.h"
      13             : 
      14             : registerMooseObject("MooseApp", GhostingUserObject);
      15             : 
      16             : namespace
      17             : {
      18             : constexpr unsigned int GEOMETRIC_MAP_IDX = 0;
      19             : constexpr unsigned int ALGEBRAIC_MAP_IDX = 1;
      20             : }
      21             : 
      22             : InputParameters
      23       14487 : GhostingUserObject::validParams()
      24             : {
      25       14487 :   InputParameters params = GeneralUserObject::validParams();
      26             : 
      27       14487 :   params.addParam<std::vector<processor_id_type>>("pids", "The PID(s) to see the ghosting for");
      28             : 
      29             :   // No need to set or override this flag, this object is triggered by meshChanged events
      30       28974 :   params.set<ExecFlagEnum>("execute_on") = {EXEC_INITIAL};
      31             : 
      32       14487 :   params.addClassDescription("Creates ghosting maps that can be queried by external objects.");
      33       14487 :   return params;
      34       14487 : }
      35             : 
      36         111 : GhostingUserObject::GhostingUserObject(const InputParameters & parameters)
      37             :   : GeneralUserObject(parameters),
      38         111 :     _mesh(_subproblem.mesh()),
      39         222 :     _nl(_fe_problem.getNonlinearSystemBase(_sys.number()))
      40             : {
      41         111 :   _maps.resize(2); // Geometric and Algebraic maps
      42             : 
      43         111 :   if (parameters.isParamValid("pids"))
      44           0 :     _pids = getParam<std::vector<processor_id_type>>("pids");
      45             :   else
      46             :   {
      47             :     // If a PIDs vector is not passed, generate patterns for all processors
      48         111 :     _pids.resize(_app.n_processors());
      49         111 :     std::iota(_pids.begin(), _pids.end(), 0);
      50             :   }
      51         111 : }
      52             : 
      53             : void
      54         111 : GhostingUserObject::initialSetup()
      55             : {
      56         111 :   meshChanged();
      57         111 : }
      58             : 
      59             : void
      60         111 : GhostingUserObject::meshChanged()
      61             : {
      62             :   // Clear the maps before rebuilding
      63         333 :   for (auto & map_ref : _maps)
      64         222 :     for (auto pid_map_pair : map_ref)
      65           0 :       pid_map_pair.second.clear();
      66             : 
      67         552 :   for (auto pid : _pids)
      68             :   {
      69         441 :     auto begin_elem = _mesh.getMesh().active_pid_elements_begin(pid);
      70         441 :     const auto end_elem = _mesh.getMesh().active_pid_elements_end(pid);
      71             : 
      72             :     // Loop over all the geometric ghosting functors and build up a map of elements that are ghosted
      73             :     // for each PID
      74         441 :     libMesh::GhostingFunctor::map_type geometric_elems;
      75         882 :     for (auto & gf : as_range(_mesh.getMesh().ghosting_functors_begin(),
      76        4113 :                               _mesh.getMesh().ghosting_functors_end()))
      77        2790 :       (*gf)(begin_elem, end_elem, pid, geometric_elems);
      78         441 :     _maps[GEOMETRIC_MAP_IDX].emplace(pid, geometric_elems);
      79             : 
      80             :     // Loop over all the algebraic ghosting functors and build up a map of elements that are ghosted
      81             :     // for each PID
      82         441 :     libMesh::GhostingFunctor::map_type algebraic_elems;
      83         882 :     for (auto & gf : as_range(_nl.dofMap().algebraic_ghosting_functors_begin(),
      84        2061 :                               _nl.dofMap().algebraic_ghosting_functors_end()))
      85         738 :       (*gf)(begin_elem, end_elem, pid, algebraic_elems);
      86         441 :     for (auto & gf :
      87        1755 :          as_range(_nl.dofMap().coupling_functors_begin(), _nl.dofMap().coupling_functors_end()))
      88         873 :       (*gf)(begin_elem, end_elem, pid, algebraic_elems);
      89         441 :     _maps[ALGEBRAIC_MAP_IDX].emplace(pid, algebraic_elems);
      90         441 :   }
      91         111 : }
      92             : 
      93             : Real
      94       29312 : GhostingUserObject::getElementalValue(const Elem * elem,
      95             :                                       Moose::RelationshipManagerType rm_type,
      96             :                                       processor_id_type pid) const
      97             : {
      98       29312 :   unsigned int map_idx = (rm_type == Moose::RelationshipManagerType::GEOMETRIC ? GEOMETRIC_MAP_IDX
      99             :                                                                                : ALGEBRAIC_MAP_IDX);
     100             : 
     101       29312 :   auto map_it = _maps[map_idx].find(pid);
     102       29312 :   if (map_it == _maps[map_idx].end())
     103           0 :     mooseError("No entry in the ghosting map for processor ID: ", pid);
     104             : 
     105       29312 :   auto map_ref = map_it->second;
     106       29312 :   if (map_ref.find(elem) != map_ref.end())
     107        3554 :     return 1.;
     108             :   else
     109       25758 :     return 0.;
     110       29312 : }

Generated by: LCOV version 1.14