https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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"
13
14#include "libmesh/mesh_base.h"
15#include "libmesh/elem.h"
16
18
21{
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{
48 if (MooseUtils::absoluteFuzzyLessEqual(_max, _min))
49 paramError("max_value", "Maximum value should be above minimum value.");
51}
52
53void
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
62 _mesh_fully_indexed = false;
63}
64
65unsigned int
66FunctorBinnedValuesDivision::getBinIndex(Real value, const Point & pt) const
67{
68 // Handle out of bounds functor values
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
96unsigned int
98{
99 Moose::ElemArg elem_arg = {&elem, false};
101 return getBinIndex(_functor(elem_arg, time_arg), elem.vertex_average());
102}
103
104unsigned 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}
registerMooseObject("MooseApp", FunctorBinnedValuesDivision)
void ErrorVector unsigned int
Divides the mesh based on the binned values of a functor.
unsigned int getBinIndex(Real value, const Point &pt) const
Get the bin for that functor value.
const Real _max
Max functor bin value.
const Real _min
Min functor bin value.
const bool _oob_is_edge_bins
Whether to map functor values outside [min, max] onto the edge bins.
virtual void initialize() override
Set up any data members that would be necessary to obtain the division indices.
const Moose::Functor< Real > & _functor
Functor to use to subdivide the mesh.
const unsigned int _nbins
Number of value bins.
FunctorBinnedValuesDivision(const InputParameters &parameters)
virtual unsigned int divisionIndex(const Point &pt) const override
Return the index of the division to which the point belongs.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addRequiredRangeCheckedParam(const std::string &name, const std::string &parsed_function, const std::string &doc_string)
These methods add an range checked parameters.
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.
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...
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.
Base class for MeshDivision objects.
static InputParameters validParams()
Class constructor.
void setNumDivisions(const unsigned int ndivs)
Set the number of divisions.
const MooseMesh & _mesh
Mesh that is being divided.
bool _mesh_fully_indexed
Whether the mesh is fully covered / indexed, all elements and points have a valid index.
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:457
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:271
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition MooseMesh.C:3549
An interface for accessing Moose::Functors for systems that do not care about automatic differentiati...
void mooseWarning(Args &&... args) const
unsigned int INVALID_DIVISION_INDEX
Invalid subdomain id to return when outside the mesh division.
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64
const unsigned int invalid_uint
A structure that is used to evaluate Moose functors logically at an element/cell center.
A structure that is used to evaluate Moose functors at an arbitrary physical point contained within a...
State argument for evaluating functors.