https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ElementQualityChecker.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 "MooseError.h"
12#include "Conversion.h"
13
14#include "libmesh/elem_quality.h"
15#include "libmesh/enum_elem_quality.h"
16#include "libmesh/string_to_enum.h"
17
18#include <limits>
19
22{
23 return MooseEnum("ASPECT_RATIO SKEW SHEAR SHAPE MAX_ANGLE MIN_ANGLE CONDITION DISTORTION TAPER "
24 "WARP STRETCH DIAGONAL ASPECT_RATIO_BETA ASPECT_RATIO_GAMMA SIZE JACOBIAN "
25 "TWIST EDGE_LENGTH_RATIO MAX_DIHEDRAL_ANGLE MIN_DIHEDRAL_ANGLE SCALED_JACOBIAN");
26}
27
30{
31 return MooseEnum("WARNING ERROR", "WARNING");
32}
33
35
38{
40 params.addClassDescription("Class to check the quality of each element using different metrics "
41 "from libmesh.");
42
43 params.addRequiredParam<MooseEnum>("metric_type",
45 "Type of quality metric to be checked");
46 params.addParam<Real>("upper_bound", "The upper bound for provided metric type");
47 params.addParam<Real>("lower_bound", "The lower bound for provided metric type");
48 params.addParam<bool>("suppress_invalid_metric_warning",
49 false,
50 "Whether to print the warning related to the quality metric type not being "
51 "applicable to a given element type.");
52 params.addParam<MooseEnum>("failure_type",
54 "The way how the failure of quality metric check should respond");
55 params.set<ExecFlagEnum>("execute_on") = EXEC_INITIAL;
56
57 return params;
58}
59
61 : ElementUserObject(parameters),
62 _m_type(getParam<MooseEnum>("metric_type").getEnum<libMesh::ElemQuality>()),
63 _has_upper_bound(isParamValid("upper_bound")),
64 _has_lower_bound(isParamValid("lower_bound")),
65 _upper_bound(_has_upper_bound ? getParam<Real>("upper_bound") : 0.0),
66 _lower_bound(_has_lower_bound ? getParam<Real>("lower_bound") : 0.0),
67 _m_min(std::numeric_limits<Real>::max()),
68 _m_max(std::numeric_limits<Real>::lowest()),
69 _m_sum(0),
70 _suppress_invalid_metric_warning(getParam<bool>("suppress_invalid_metric_warning")),
71 _failure_type(getParam<MooseEnum>("failure_type").getEnum<FailureType>())
72{
73}
74
75void
77{
78 _m_min = std::numeric_limits<Real>::max();
79 _m_max = std::numeric_limits<Real>::lowest();
80 _m_sum = 0;
82 _elem_ids.clear();
83 _bypassed = false;
84 _bypassed_elem_type.clear();
85}
86
87void
89{
90 // obtain the available quality metric for current ElemType
91 std::vector<libMesh::ElemQuality> metrics_avail = libMesh::Quality::valid(_current_elem->type());
92
93 // check whether the provided quality metric is applicable to current ElemType
94 if (!checkMetricApplicability(_m_type, metrics_avail))
95 {
96 _bypassed = true;
98
99 return;
100 }
101
102 std::pair<Real, Real> default_bounds = _current_elem->qual_bounds(_m_type);
103 std::pair<Real, Real> actual_bounds;
105 {
107 mooseError("Provided lower bound should be less than provided upper bound!");
108
109 actual_bounds = std::make_pair(_lower_bound, _upper_bound);
110 }
111 else if (_has_lower_bound)
112 {
113 if (_lower_bound >= default_bounds.second)
114 mooseError("Provided lower bound should less than the default upper bound: ",
115 default_bounds.second);
116
117 actual_bounds = std::make_pair(_lower_bound, default_bounds.second);
118 }
119 else if (_has_upper_bound)
120 {
121 if (_upper_bound <= default_bounds.first)
122 mooseError("Provided upper bound should larger than the default lower bound: ",
123 default_bounds.first);
124
125 actual_bounds = std::make_pair(default_bounds.first, _upper_bound);
126 }
127 else
128 actual_bounds = default_bounds;
129
130 // calculate and save quality metric value for current element
131 Real mv = _current_elem->quality(_m_type);
132
134 _m_sum += mv;
135 if (mv < _m_min)
136 _m_min = mv;
137 if (mv > _m_max)
138 _m_max = mv;
139
140 // check element quality metric, save ids of elements whose quality metrics exceeds the preset
141 // bounds
142 if (mv < actual_bounds.first || mv > actual_bounds.second)
143 _elem_ids.insert(_current_elem->id());
144}
145
146void
148{
149 const auto & eqc = cast_ref<const ElementQualityChecker &>(uo);
150 _elem_ids.insert(eqc._elem_ids.begin(), eqc._elem_ids.end());
151 _bypassed_elem_type.insert(eqc._bypassed_elem_type.begin(), eqc._bypassed_elem_type.end());
152 _bypassed |= eqc._bypassed;
153 _m_sum += eqc._m_sum;
154 _checked_elem_num += eqc._checked_elem_num;
155
156 if (_m_min > eqc._m_min)
157 _m_min = eqc._m_min;
158 if (_m_max < eqc._m_max)
159 _m_max = eqc._m_max;
160}
161
162void
164{
172
173 if (_bypassed)
175 mooseWarning("Provided quality metric doesn't apply to following element type: " +
177
178 _console << libMesh::Quality::name(_m_type) << " Metric values:\n";
180 {
181 _console << " Minimum: " << _m_min << "\n";
182 _console << " Maximum: " << _m_max << "\n";
183 _console << " Average: " << _m_sum / _checked_elem_num << "\n";
184 }
185 else
186 _console << " No elements were checked.\n";
187
188 if (!_elem_ids.empty())
189 {
190 switch (_failure_type)
191 {
193 {
194 mooseWarning("List of failed element IDs: ", Moose::stringify(_elem_ids));
195 break;
196 }
197
199 {
200 mooseError("List of failed element IDs: ", Moose::stringify(_elem_ids));
201 break;
202 }
203
204 default:
205 mooseError("Unknown failure type!");
206 }
207 }
208
209 _console << std::flush;
210}
211
212bool
214 const libMesh::ElemQuality & elem_metric,
215 const std::vector<libMesh::ElemQuality> & elem_metrics)
216{
217 bool has_metric = false;
218
219 for (unsigned int i = 0; i < elem_metrics.size(); ++i)
220 if (elem_metric == elem_metrics[i])
221 has_metric = true;
222
223 return has_metric;
224}
registerMooseObject("MooseApp", ElementQualityChecker)
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition MooseError.h:345
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
const ExecFlagType EXEC_INITIAL
Definition Moose.C:31
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
ElementQualityChecker(const InputParameters &parameters)
libMesh::ElemQuality _m_type
void threadJoin(const UserObject &uo) override
Must override.
bool checkMetricApplicability(const libMesh::ElemQuality &elem_metric, const std::vector< libMesh::ElemQuality > &elem_metrics)
static MooseEnum QualityMetricType()
void execute() override
Execute method.
void initialize() override
Called before execute() is ever called so that data can be cleared.
const bool _suppress_invalid_metric_warning
Whether to print element applicability warning for bypassed elements.
static MooseEnum FailureMessageType()
void finalize() override
Finalize.
std::set< dof_id_type > _elem_ids
set to save ids for all failed elements
static InputParameters validParams()
bool _bypassed
whether the element quality check is bypassed or not
Real _m_min
minimum, maximum and summation of quality metric values of all checked elements
std::set< std::string > _bypassed_elem_type
set to save bypassed element type
const FailureType _failure_type
unsigned int _checked_elem_num
number of checked elements
static InputParameters validParams()
const Elem *const & _current_elem
The current element pointer (available during execute())
A MultiMooseEnum object to hold "execute_on" flags.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
void max(const T &r, T &o, Request &req) const
void min(const T &r, T &o, Request &req) const
void set_union(T &data, const unsigned int root_id) const
Base class for user-specific data.
Definition UserObject.h:20
const Parallel::Communicator & _communicator
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:65
std::string name(const ElemQuality q)
std::vector< ElemQuality > valid(const ElemType t)
std::string enum_to_string(const T e)
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...