LCOV - code coverage report
Current view: top level - src/loops - ComputeIndicatorThread.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 112 112 100.0 %
Date: 2025-07-17 01:28:37 Functions: 13 13 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 "ComputeIndicatorThread.h"
      11             : 
      12             : // MOOSE includes
      13             : #include "AuxiliarySystem.h"
      14             : #include "FEProblem.h"
      15             : #include "Indicator.h"
      16             : #include "InternalSideIndicatorBase.h"
      17             : #include "MooseVariableFE.h"
      18             : #include "Problem.h"
      19             : #include "SwapBackSentinel.h"
      20             : // For dynamic casting to Coupleable
      21             : #include "Material.h"
      22             : #include "InterfaceMaterial.h"
      23             : 
      24             : #include "libmesh/threads.h"
      25             : 
      26        4722 : ComputeIndicatorThread::ComputeIndicatorThread(FEProblemBase & fe_problem, bool finalize)
      27             :   : ThreadedElementLoop<ConstElemRange>(fe_problem),
      28        4722 :     _fe_problem(fe_problem),
      29        9444 :     _aux_sys(fe_problem.getAuxiliarySystem()),
      30        4722 :     _indicator_whs(_fe_problem.getIndicatorWarehouse()),
      31        4722 :     _internal_side_indicators(_fe_problem.getInternalSideIndicatorWarehouse()),
      32        4722 :     _finalize(finalize)
      33             : {
      34        4722 : }
      35             : 
      36             : // Splitting Constructor
      37         480 : ComputeIndicatorThread::ComputeIndicatorThread(ComputeIndicatorThread & x, Threads::split split)
      38             :   : ThreadedElementLoop<ConstElemRange>(x, split),
      39         480 :     _fe_problem(x._fe_problem),
      40         480 :     _aux_sys(x._aux_sys),
      41         480 :     _indicator_whs(x._indicator_whs),
      42         480 :     _internal_side_indicators(x._internal_side_indicators),
      43         480 :     _finalize(x._finalize)
      44             : {
      45         480 : }
      46             : 
      47        5682 : ComputeIndicatorThread::~ComputeIndicatorThread() {}
      48             : 
      49             : void
      50       26430 : ComputeIndicatorThread::subdomainChanged()
      51             : {
      52       26430 :   _fe_problem.subdomainSetup(_subdomain, _tid);
      53             : 
      54       26430 :   _indicator_whs.subdomainSetup(_tid);
      55       26430 :   _internal_side_indicators.subdomainSetup(_tid);
      56             : 
      57       26430 :   std::set<MooseVariableFEBase *> needed_moose_vars;
      58       26430 :   _indicator_whs.updateVariableDependency(needed_moose_vars, _tid);
      59       26430 :   _internal_side_indicators.updateVariableDependency(needed_moose_vars, _tid);
      60       26430 :   _fe_problem.setActiveElementalMooseVariables(needed_moose_vars, _tid);
      61             : 
      62             :   // Update variable coupleable vector tags
      63       26430 :   std::set<TagID> needed_var_vector_tags;
      64       26430 :   _indicator_whs.updateBlockFEVariableCoupledVectorTagDependency(
      65       26430 :       _subdomain, needed_var_vector_tags, _tid);
      66       26430 :   _internal_side_indicators.updateBlockFEVariableCoupledVectorTagDependency(
      67       26430 :       _subdomain, needed_var_vector_tags, _tid);
      68       26430 :   _fe_problem.getMaterialWarehouse().updateBlockFEVariableCoupledVectorTagDependency(
      69       26430 :       _subdomain, needed_var_vector_tags, _tid);
      70       26430 :   _fe_problem.setActiveFEVariableCoupleableVectorTags(needed_var_vector_tags, _tid);
      71             : 
      72       26430 :   std::unordered_set<unsigned int> needed_mat_props;
      73       26430 :   _indicator_whs.updateMatPropDependency(needed_mat_props, _tid);
      74       26430 :   _internal_side_indicators.updateMatPropDependency(needed_mat_props, _tid);
      75             : 
      76       26430 :   _fe_problem.prepareMaterials(needed_mat_props, _subdomain, _tid);
      77       26430 : }
      78             : 
      79             : void
      80     1589106 : ComputeIndicatorThread::onElement(const Elem * elem)
      81             : {
      82     5675980 :   for (auto * var : _aux_sys._elem_vars[_tid])
      83     4086874 :     var->prepareAux();
      84             : 
      85     1589106 :   _fe_problem.prepare(elem, _tid);
      86     1589106 :   _fe_problem.reinitElem(elem, _tid);
      87             : 
      88             :   // Set up Sentinel class so that, even if reinitMaterials() throws, we
      89             :   // still remember to swap back during stack unwinding.
      90     1589106 :   SwapBackSentinel sentinel(_fe_problem, &FEProblemBase::swapBackMaterials, _tid);
      91             : 
      92     1589106 :   _fe_problem.reinitMaterials(_subdomain, _tid);
      93             : 
      94             :   // Compute
      95     1589106 :   if (!_finalize)
      96             :   {
      97      794553 :     if (_indicator_whs.hasActiveBlockObjects(_subdomain, _tid))
      98             :     {
      99             :       const std::vector<std::shared_ptr<Indicator>> & indicators =
     100       36956 :           _indicator_whs.getActiveBlockObjects(_subdomain, _tid);
     101       73912 :       for (const auto & indicator : indicators)
     102       36956 :         indicator->computeIndicator();
     103             :     }
     104             :   }
     105             : 
     106             :   // Finalize
     107             :   else
     108             :   {
     109      794553 :     if (_indicator_whs.hasActiveBlockObjects(_subdomain, _tid))
     110             :     {
     111             :       const std::vector<std::shared_ptr<Indicator>> & indicators =
     112       36956 :           _indicator_whs.getActiveBlockObjects(_subdomain, _tid);
     113       73912 :       for (const auto & indicator : indicators)
     114       36956 :         indicator->finalize();
     115             :     }
     116             : 
     117      794553 :     if (_internal_side_indicators.hasActiveBlockObjects(_subdomain, _tid))
     118             :     {
     119             :       const std::vector<std::shared_ptr<InternalSideIndicatorBase>> & internal_indicators =
     120      781945 :           _internal_side_indicators.getActiveBlockObjects(_subdomain, _tid);
     121     1565490 :       for (const auto & internal_indicator : internal_indicators)
     122      783545 :         internal_indicator->finalize();
     123             :     }
     124             :   }
     125             : 
     126     1589106 :   if (!_finalize) // During finalize the Indicators should be setting values in the vectors manually
     127             :   {
     128      794553 :     Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
     129     2837990 :     for (auto * var : _aux_sys._elem_vars[_tid])
     130     2043437 :       var->add(_aux_sys.solution());
     131      794553 :   }
     132     1589106 : }
     133             : 
     134             : void
     135      253674 : ComputeIndicatorThread::onBoundary(const Elem * /*elem*/,
     136             :                                    unsigned int /*side*/,
     137             :                                    BoundaryID /*bnd_id*/,
     138             :                                    const Elem * /*lower_d_elem = nullptr*/)
     139             : {
     140      253674 : }
     141             : 
     142             : void
     143     3180850 : ComputeIndicatorThread::onInternalSide(const Elem * elem, unsigned int side)
     144             : {
     145     3180850 :   if (_finalize) // If finalizing we only do something on the elements
     146     1590425 :     return;
     147             : 
     148             :   // Pointer to the neighbor we are currently working on.
     149     1590425 :   const Elem * neighbor = elem->neighbor_ptr(side);
     150             : 
     151     5698137 :   for (auto * var : _aux_sys._elem_vars[_tid])
     152     4107712 :     var->prepareAux();
     153             : 
     154     1590425 :   SubdomainID block_id = elem->subdomain_id();
     155     1590425 :   if (_internal_side_indicators.hasActiveBlockObjects(block_id, _tid))
     156             :   {
     157     1567340 :     _fe_problem.reinitNeighbor(elem, side, _tid);
     158             : 
     159             :     // Set up Sentinels so that, even if one of the reinitMaterialsXXX() calls throws, we
     160             :     // still remember to swap back during stack unwinding.
     161     1567340 :     SwapBackSentinel face_sentinel(_fe_problem, &FEProblemBase::swapBackMaterialsFace, _tid);
     162     1567340 :     _fe_problem.reinitMaterialsFace(block_id, _tid);
     163             : 
     164             :     SwapBackSentinel neighbor_sentinel(
     165     1567340 :         _fe_problem, &FEProblemBase::swapBackMaterialsNeighbor, _tid);
     166     1567340 :     _fe_problem.reinitMaterialsNeighbor(neighbor->subdomain_id(), _tid);
     167             : 
     168             :     const std::vector<std::shared_ptr<InternalSideIndicatorBase>> & indicators =
     169     1567340 :         _internal_side_indicators.getActiveBlockObjects(block_id, _tid);
     170     3137560 :     for (const auto & indicator : indicators)
     171     1570220 :       indicator->computeIndicator();
     172     1567340 :   }
     173             : }
     174             : 
     175             : void
     176     1589106 : ComputeIndicatorThread::postElement(const Elem * /*elem*/)
     177             : {
     178     1589106 : }
     179             : 
     180             : void
     181        5202 : ComputeIndicatorThread::post()
     182             : {
     183        5202 :   _fe_problem.clearActiveElementalMooseVariables(_tid);
     184        5202 :   _fe_problem.clearActiveMaterialProperties(_tid);
     185        5202 : }
     186             : 
     187             : void
     188         480 : ComputeIndicatorThread::join(const ComputeIndicatorThread & /*y*/)
     189             : {
     190         480 : }
     191             : 
     192             : void
     193        5202 : ComputeIndicatorThread::printGeneralExecutionInformation() const
     194             : {
     195        5202 :   if (!_fe_problem.shouldPrintExecution(_tid))
     196        5062 :     return;
     197             : 
     198         140 :   const auto & console = _fe_problem.console();
     199         140 :   const auto & execute_on = _fe_problem.getCurrentExecuteOnFlag();
     200         140 :   if (!_finalize)
     201          70 :     console << "[DBG] Executing indicators on elements then on internal sides on " << execute_on
     202          70 :             << std::endl;
     203             :   else
     204          70 :     console << "[DBG] Finalizing indicator loop" << std::endl;
     205             : }
     206             : 
     207             : void
     208       26430 : ComputeIndicatorThread::printBlockExecutionInformation() const
     209             : {
     210       26430 :   if (!_fe_problem.shouldPrintExecution(_tid) || _blocks_exec_printed.count(_subdomain))
     211       26290 :     return;
     212             : 
     213         140 :   const auto & console = _fe_problem.console();
     214         140 :   if (_indicator_whs.hasActiveBlockObjects(_subdomain, _tid))
     215             :   {
     216          80 :     const auto & indicators = _indicator_whs.getActiveBlockObjects(_subdomain, _tid);
     217          80 :     console << "[DBG] Ordering of element indicators on block " << _subdomain << std::endl;
     218          80 :     printExecutionOrdering<Indicator>(indicators, false);
     219             :   }
     220         140 :   if (_internal_side_indicators.hasActiveBlockObjects(_subdomain, _tid))
     221             :   {
     222         140 :     const auto & indicators = _internal_side_indicators.getActiveBlockObjects(_subdomain, _tid);
     223         140 :     console << "[DBG] Ordering of element internal sides indicators on block " << _subdomain
     224         140 :             << std::endl;
     225         140 :     printExecutionOrdering<InternalSideIndicatorBase>(indicators, false);
     226             :   }
     227         140 :   _blocks_exec_printed.insert(_subdomain);
     228             : }

Generated by: LCOV version 1.14