<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

#include "FVAnisotropicDiffusion.h"
#include "RelationshipManager.h"

registerMooseObject("MooseApp", FVAnisotropicDiffusion);

InputParameters
FVAnisotropicDiffusion::validParams()
{
  InputParameters params = FVFluxKernel::validParams();
  params.addClassDescription(
      "Computes residual for anisotropic diffusion operator for finite volume method.");
  params.addRequiredParam<MooseFunctorName>("coeff", "diffusion coefficient");
  params.set<unsigned short>("ghost_layers") = 2;
  return params;
}

FVAnisotropicDiffusion::FVAnisotropicDiffusion(const InputParameters & params)
  : FVFluxKernel(params), _coeff(getFunctor<ADRealVectorValue>("coeff"))
{
#ifndef MOOSE_GLOBAL_AD_INDEXING
  mooseError("FVAnisotropicDiffusion is not supported by local AD indexing. In order to use this "
             "object, please run "
             "the configure script in the root MOOSE directory with the configure option "
             "'--with-ad-indexing-type=global'. Note that global indexing is now the default "
             "configuration for AD indexing type.");
#endif

  if ((_var.faceInterpolationMethod() == Moose::FV::InterpMethod::SkewCorrectedAverage) &&
      (_tid == 0))
  {
    auto & factory = _app.getFactory();

    auto rm_params = factory.getValidParams("ElementSideNeighborLayers");

    rm_params.set<std::string>("for_whom") = name();
    rm_params.set<MooseMesh *>("mesh") = &const_cast<MooseMesh &>(_mesh);
    rm_params.set<Moose::RelationshipManagerType>("rm_type") =
        Moose::RelationshipManagerType::GEOMETRIC | Moose::RelationshipManagerType::ALGEBRAIC |
        Moose::RelationshipManagerType::COUPLING;
    FVKernel::setRMParams(
        _pars, rm_params, std::max((unsigned short)(3), _pars.get<unsigned short>("ghost_layers")));
    mooseAssert(rm_params.areAllRequiredParamsValid(),
                "All relationship manager parameters should be valid.");

    auto rm_obj = factory.create<RelationshipManager>(
        "ElementSideNeighborLayers", name() + "_skew_correction", rm_params);

    // Delete the resources created on behalf of the RM if it ends up not being added to the
    // App.
    if (!_app.addRelationshipManager(rm_obj))
      factory.releaseSharedObjects(*rm_obj);
  }
}

ADReal
FVAnisotropicDiffusion::computeQpResidual()
{
  ADReal r = 0;
  const auto face_elem = elemFromFace();
  const auto face_neighbor = neighborFromFace();
  const auto & grad_T = _var.adGradSln(
      *_face_info, _var.faceInterpolationMethod() == Moose::FV::InterpMethod::SkewCorrectedAverage);
  ADRealVectorValue k_face_inv, k_elem_inv, k_neigh_inv;
  const auto k_elem = _coeff(face_elem), k_neighbor = _coeff(face_neighbor);
  for (const auto i : make_range(Moose::dim))
  {
    k_elem_inv(i) = 1.0 / k_elem(i);
    k_neigh_inv(i) = 1.0 / k_neighbor(i);
  }

  Moose::FV::interpolate(
      Moose::FV::InterpMethod::Average, k_face_inv, k_elem_inv, k_neigh_inv, *_face_info, true);
  for (const auto i : make_range(Moose::dim))
    r += _normal(i) * grad_T(i) / k_face_inv(i);
  return -r;
}<commit_msg>FPE in test caused by i=2 (z-direction) in 2D problem, changing loop indexing (#21832)<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

#include "FVAnisotropicDiffusion.h"
#include "RelationshipManager.h"

registerMooseObject("MooseApp", FVAnisotropicDiffusion);

InputParameters
FVAnisotropicDiffusion::validParams()
{
  InputParameters params = FVFluxKernel::validParams();
  params.addClassDescription(
      "Computes residual for anisotropic diffusion operator for finite volume method.");
  params.addRequiredParam<MooseFunctorName>("coeff", "diffusion coefficient");
  params.set<unsigned short>("ghost_layers") = 2;
  return params;
}

FVAnisotropicDiffusion::FVAnisotropicDiffusion(const InputParameters & params)
  : FVFluxKernel(params), _coeff(getFunctor<ADRealVectorValue>("coeff"))
{
#ifndef MOOSE_GLOBAL_AD_INDEXING
  mooseError("FVAnisotropicDiffusion is not supported by local AD indexing. In order to use this "
             "object, please run "
             "the configure script in the root MOOSE directory with the configure option "
             "'--with-ad-indexing-type=global'. Note that global indexing is now the default "
             "configuration for AD indexing type.");
#endif

  if ((_var.faceInterpolationMethod() == Moose::FV::InterpMethod::SkewCorrectedAverage) &&
      (_tid == 0))
  {
    auto & factory = _app.getFactory();

    auto rm_params = factory.getValidParams("ElementSideNeighborLayers");

    rm_params.set<std::string>("for_whom") = name();
    rm_params.set<MooseMesh *>("mesh") = &const_cast<MooseMesh &>(_mesh);
    rm_params.set<Moose::RelationshipManagerType>("rm_type") =
        Moose::RelationshipManagerType::GEOMETRIC | Moose::RelationshipManagerType::ALGEBRAIC |
        Moose::RelationshipManagerType::COUPLING;
    FVKernel::setRMParams(
        _pars, rm_params, std::max((unsigned short)(3), _pars.get<unsigned short>("ghost_layers")));
    mooseAssert(rm_params.areAllRequiredParamsValid(),
                "All relationship manager parameters should be valid.");

    auto rm_obj = factory.create<RelationshipManager>(
        "ElementSideNeighborLayers", name() + "_skew_correction", rm_params);

    // Delete the resources created on behalf of the RM if it ends up not being added to the
    // App.
    if (!_app.addRelationshipManager(rm_obj))
      factory.releaseSharedObjects(*rm_obj);
  }
}

ADReal
FVAnisotropicDiffusion::computeQpResidual()
{
  ADReal r = 0;
  const auto face_elem = elemFromFace();
  const auto face_neighbor = neighborFromFace();
  const auto & grad_T = _var.adGradSln(
      *_face_info, _var.faceInterpolationMethod() == Moose::FV::InterpMethod::SkewCorrectedAverage);
  ADRealVectorValue k_face_inv, k_elem_inv, k_neigh_inv;
  const auto k_elem = _coeff(face_elem), k_neighbor = _coeff(face_neighbor);
  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
  {
    mooseAssert(k_elem(i) != 0, "Diffusion coefficient is equal to zero");
    mooseAssert(k_neighbor(i) != 0, "Diffusion coefficient is equal to zero");
    k_elem_inv(i) = 1.0 / k_elem(i);
    k_neigh_inv(i) = 1.0 / k_neighbor(i);
  }

  Moose::FV::interpolate(
      Moose::FV::InterpMethod::Average, k_face_inv, k_elem_inv, k_neigh_inv, *_face_info, true);
  for (const auto i : make_range(Moose::dim))
    r += _normal(i) * grad_T(i) / k_face_inv(i);
  return -r;
}
{'fraction_non_alphanumeric': 0.1240343347639485, 'fraction_numerical': 0.004864091559370529, 'mean_word_length': 4.5528196981731535, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5100321', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2265, 'n_words': 498}