https://mooseframework.inl.gov
FunctorBinnedValuesDivision.C
Go to the documentation of this file.
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 
11 #include "MooseMesh.h"
12 #include "MooseFunctorArguments.h"
13 
14 #include "libmesh/mesh_base.h"
15 #include "libmesh/elem.h"
16 
18 
21 {
23  params.addClassDescription(
24  "Divide the mesh along based on uniformly binned values of a functor.");
25  params.addRequiredParam<Real>("min_value", "Minimum value of the functor for the binning");
26  params.addRequiredParam<Real>("max_value", "Maximum value of the functor for the binning");
27  params.addRequiredRangeCheckedParam<unsigned int>(
28  "num_bins", "num_bins>0", "Number of uniform bins in functor values");
29  params.addRequiredParam<MooseFunctorName>(
30  "functor", "Functor to evaluate to assign points/elements to the bins");
31  params.addParam<bool>("assign_out_of_bounds_to_extreme_bins",
32  false,
33  "Whether to map functor values outside of the [min,max] range to the "
34  "lowest and highest bins, or to use the invalid division index");
35 
36  return params;
37 }
38 
40  : MeshDivision(parameters),
42  _min(getParam<Real>("min_value")),
43  _max(getParam<Real>("max_value")),
44  _nbins(getParam<unsigned int>("num_bins")),
45  _functor(getFunctor<Real>("functor")),
46  _oob_is_edge_bins(getParam<bool>("assign_out_of_bounds_to_extreme_bins"))
47 {
49  paramError("max_value", "Maximum value should be above minimum value.");
51 }
52 
53 void
55 {
57 
58  // We could alternatively check every point in the mesh but it seems expensive
59  // the functor values can also change so this check would need to be done regularly
60  _mesh_fully_indexed = true;
61  if (!_oob_is_edge_bins)
62  _mesh_fully_indexed = false;
63 }
64 
65 unsigned int
66 FunctorBinnedValuesDivision::getBinIndex(Real value, const Point & pt) const
67 {
68  // Handle out of bounds functor values
69  if (!_oob_is_edge_bins)
70  {
71  if (value < _min)
73  else if (value > _max)
75  }
76  else
77  {
78  if (value < _min)
79  return 0;
80  else if (value > _max)
81  return _nbins - 1;
82  }
83 
84  for (const auto i_bin : make_range(_nbins + 1))
85  {
86  const auto border_value = _min + i_bin / _nbins * (_max - _min);
87  if (MooseUtils::absoluteFuzzyEqual(value, border_value))
88  mooseWarning("Functor value " + std::to_string(value) +
89  " is on a bin edge for evaluation near " + Moose::stringify(pt));
90  if (value < border_value)
91  return (i_bin > 0) ? i_bin - 1 : 0;
92  }
93  return _nbins;
94 }
95 
96 unsigned int
98 {
99  Moose::ElemArg elem_arg = {&elem, false};
101  return getBinIndex(_functor(elem_arg, time_arg), elem.vertex_average());
102 }
103 
104 unsigned int
106 {
108 
109  const auto & pl = _mesh.getMesh().sub_point_locator();
110  // There could be more than one elem if we are on the edge between two elements
111  std::set<const Elem *> candidates;
112  (*pl)(pt, candidates);
113 
114  // By convention we will use the element with the lowest element id
115  const Elem * elem = nullptr;
116  unsigned int min_elem_id = libMesh::invalid_uint;
117  for (const auto elem_ptr : candidates)
118  if (elem_ptr->id() < min_elem_id)
119  {
120  elem = elem_ptr;
121  min_elem_id = elem_ptr->id();
122  }
123  if (!elem)
124  mooseError("Division index queried for a point outside the local mesh");
125  Moose::ElemPointArg elem_pt_arg = {elem, pt, false};
126 
127  // Find the element with the lowest id to form an ElemPt argument
128  return getBinIndex(_functor(elem_pt_arg, time_arg), pt);
129 }
An interface for accessing Moose::Functors for systems that do not care about automatic differentiati...
unsigned int getBinIndex(Real value, const Point &pt) const
Get the bin for that functor value.
void addRequiredRangeCheckedParam(const std::string &name, const std::string &parsed_function, const std::string &doc_string)
These methods add an range checked parameters.
Divides the mesh based on the binned values of a functor.
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
Definition: MooseUtils.h:380
const unsigned int invalid_uint
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:435
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const unsigned int _nbins
Number of value bins.
static InputParameters validParams()
A structure that is used to evaluate Moose functors at an arbitrary physical point contained within a...
Base class for MeshDivision objects.
Definition: MeshDivision.h:35
virtual void initialize() override
Set up any data members that would be necessary to obtain the division indices.
const MooseMesh & _mesh
Mesh that is being divided.
Definition: MeshDivision.h:74
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
const Real _max
Max functor bin value.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3448
const Moose::Functor< Real > & _functor
Functor to use to subdivide the mesh.
virtual unsigned int divisionIndex(const Point &pt) const override
Return the index of the division to which the point belongs.
const Real _min
Min functor bin value.
A structure that is used to evaluate Moose functors logically at an element/cell center.
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
unsigned int INVALID_DIVISION_INDEX
Invalid subdomain id to return when outside the mesh division.
Definition: MeshDivision.h:28
bool _mesh_fully_indexed
Whether the mesh is fully covered / indexed, all elements and points have a valid index...
Definition: MeshDivision.h:77
void setNumDivisions(const unsigned int ndivs)
Set the number of divisions.
Definition: MeshDivision.h:65
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
FunctorBinnedValuesDivision(const InputParameters &parameters)
bool absoluteFuzzyLessEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than or equal to another variable within an absolute tol...
Definition: MooseUtils.h:452
void mooseWarning(Args &&... args) const
Emits a warning prefixed with object name and type.
Definition: MooseBase.h:295
IntRange< T > make_range(T beg, T end)
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:267
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
State argument for evaluating functors.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
const bool _oob_is_edge_bins
Whether to map functor values outside [min, max] onto the edge bins.
registerMooseObject("MooseApp", FunctorBinnedValuesDivision)
static InputParameters validParams()
Class constructor.
Definition: MeshDivision.C:14
void ErrorVector unsigned int