<commit_before>//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

// MOOSE includes
#include "ComputeElemAuxBcsThread.h"
#include "AuxiliarySystem.h"
#include "FEProblem.h"
#include "DisplacedProblem.h"
#include "Assembly.h"
#include "AuxKernel.h"
#include "SwapBackSentinel.h"

#include "libmesh/threads.h"

template <typename AuxKernelType>
ComputeElemAuxBcsThread<AuxKernelType>::ComputeElemAuxBcsThread(
    FEProblemBase & problem,
    const MooseObjectWarehouse<AuxKernelType> & storage,
    const std::vector<std::vector<MooseVariableFEBase *>> & vars,
    bool need_materials)
  : _problem(problem),
    _aux_sys(problem.getAuxiliarySystem()),
    _storage(storage),
    _aux_vars(vars),
    _need_materials(need_materials)
{
}

// Splitting Constructor
template <typename AuxKernelType>
ComputeElemAuxBcsThread<AuxKernelType>::ComputeElemAuxBcsThread(ComputeElemAuxBcsThread & x,
                                                                Threads::split /*split*/)
  : _problem(x._problem),
    _aux_sys(x._aux_sys),
    _storage(x._storage),
    _aux_vars(x._aux_vars),
    _need_materials(x._need_materials)
{
}

template <typename AuxKernelType>
void
ComputeElemAuxBcsThread<AuxKernelType>::operator()(const ConstBndElemRange & range)
{
  ParallelUniqueId puid;
  _tid = puid.id;

  // Reference to all boundary restricted AuxKernels for the current thread
  const auto & boundary_kernels = _storage.getActiveBoundaryObjects(_tid);

  for (const auto & belem : range)
  {
    const Elem * elem = belem->_elem;
    unsigned short int side = belem->_side;
    BoundaryID boundary_id = belem->_bnd_id;

    // need to update the boundary ID in assembly
    _problem.setCurrentBoundaryID(boundary_id, _tid);

    if (elem->processor_id() == _problem.processor_id())
    {
      // prepare variables
      for (auto * var : _aux_vars[_tid])
        var->prepareAux();

      // Locate the AuxKernel objects for the current BoundaryID
      const auto iter = boundary_kernels.find(boundary_id);

      if (iter != boundary_kernels.end() && !(iter->second.empty()))
      {
        _problem.setCurrentSubdomainID(elem, _tid);
        _problem.prepare(elem, _tid);
        _problem.reinitElemFace(elem, side, boundary_id, _tid);

        const Elem * lower_d_elem = _problem.mesh().getLowerDElem(elem, side);
        if (lower_d_elem)
          _problem.reinitLowerDElem(lower_d_elem, _tid);

        const Elem * neighbor = elem->neighbor_ptr(side);

        // The last check here is absolutely necessary otherwise we will attempt to evaluate
        // neighbor materials on neighbor elements that aren't evaluable, e.g. don't have algebraic
        // ghosting
        bool compute_interface =
            neighbor && neighbor->active() &&
            _problem.getInterfaceMaterialsWarehouse().hasActiveBoundaryObjects(boundary_id, _tid);

        // Set up Sentinel class so that, even if reinitMaterialsFace() throws, we
        // still remember to swap back during stack unwinding.
        SwapBackSentinel sentinel(_problem, &FEProblem::swapBackMaterialsFace, _tid);
        std::unique_ptr<SwapBackSentinel> neighbor_sentinel =
            compute_interface ? std::make_unique<SwapBackSentinel>(
                                    _problem, &FEProblem::swapBackMaterialsNeighbor, _tid)
                              : nullptr;
        if (_need_materials)
        {
          std::set<unsigned int> needed_mat_props;
          for (const auto & aux : iter->second)
          {
            const std::set<unsigned int> & mp_deps = aux->getMatPropDependencies();
            needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
          }
          _problem.setActiveMaterialProperties(needed_mat_props, _tid);<commit_msg>Use predicate instead of dynamically allocating memory all the time<commit_after>//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

// MOOSE includes
#include "ComputeElemAuxBcsThread.h"
#include "AuxiliarySystem.h"
#include "FEProblem.h"
#include "DisplacedProblem.h"
#include "Assembly.h"
#include "AuxKernel.h"
#include "SwapBackSentinel.h"

#include "libmesh/threads.h"

template <typename AuxKernelType>
ComputeElemAuxBcsThread<AuxKernelType>::ComputeElemAuxBcsThread(
    FEProblemBase & problem,
    const MooseObjectWarehouse<AuxKernelType> & storage,
    const std::vector<std::vector<MooseVariableFEBase *>> & vars,
    bool need_materials)
  : _problem(problem),
    _aux_sys(problem.getAuxiliarySystem()),
    _storage(storage),
    _aux_vars(vars),
    _need_materials(need_materials)
{
}

// Splitting Constructor
template <typename AuxKernelType>
ComputeElemAuxBcsThread<AuxKernelType>::ComputeElemAuxBcsThread(ComputeElemAuxBcsThread & x,
                                                                Threads::split /*split*/)
  : _problem(x._problem),
    _aux_sys(x._aux_sys),
    _storage(x._storage),
    _aux_vars(x._aux_vars),
    _need_materials(x._need_materials)
{
}

template <typename AuxKernelType>
void
ComputeElemAuxBcsThread<AuxKernelType>::operator()(const ConstBndElemRange & range)
{
  ParallelUniqueId puid;
  _tid = puid.id;

  // Reference to all boundary restricted AuxKernels for the current thread
  const auto & boundary_kernels = _storage.getActiveBoundaryObjects(_tid);

  for (const auto & belem : range)
  {
    const Elem * elem = belem->_elem;
    unsigned short int side = belem->_side;
    BoundaryID boundary_id = belem->_bnd_id;

    // need to update the boundary ID in assembly
    _problem.setCurrentBoundaryID(boundary_id, _tid);

    if (elem->processor_id() == _problem.processor_id())
    {
      // prepare variables
      for (auto * var : _aux_vars[_tid])
        var->prepareAux();

      // Locate the AuxKernel objects for the current BoundaryID
      const auto iter = boundary_kernels.find(boundary_id);

      if (iter != boundary_kernels.end() && !(iter->second.empty()))
      {
        _problem.setCurrentSubdomainID(elem, _tid);
        _problem.prepare(elem, _tid);
        _problem.reinitElemFace(elem, side, boundary_id, _tid);

        const Elem * lower_d_elem = _problem.mesh().getLowerDElem(elem, side);
        if (lower_d_elem)
          _problem.reinitLowerDElem(lower_d_elem, _tid);

        const Elem * neighbor = elem->neighbor_ptr(side);

        // The last check here is absolutely necessary otherwise we will attempt to evaluate
        // neighbor materials on neighbor elements that aren't evaluable, e.g. don't have algebraic
        // ghosting
        bool compute_interface =
            neighbor && neighbor->active() &&
            _problem.getInterfaceMaterialsWarehouse().hasActiveBoundaryObjects(boundary_id, _tid);

        // Set up Sentinel class so that, even if reinitMaterialsFace() throws, we
        // still remember to swap back during stack unwinding.
        SwapBackSentinel sentinel(_problem, &FEProblem::swapBackMaterialsFace, _tid);
        SwapBackSentinel neighbor_sentinel(
            _problem, &FEProblem::swapBackMaterialsNeighbor, _tid, compute_interface);

        if (_need_materials)
        {
          std::set<unsigned int> needed_mat_props;
          for (const auto & aux : iter->second)
          {
            const std::set<unsigned int> & mp_deps = aux->getMatPropDependencies();
            needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
          }
          _problem.setActiveMaterialProperties(needed_mat_props, _tid);
{'fraction_non_alphanumeric': 0.10470677070224012, 'fraction_numerical': 0.0010067958721369243, 'mean_word_length': 3.2542826552462527, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 47, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2549551', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2376, 'n_words': 528}