https://mooseframework.inl.gov
SmoothMeshGenerator.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 
10 #include "SmoothMeshGenerator.h"
11 
12 // libMesh includes
13 #include "libmesh/mesh_smoother_laplace.h"
14 #include "libmesh/mesh_smoother_vsmoother.h"
15 #include "libmesh/unstructured_mesh.h"
16 #include "libmesh/replicated_mesh.h"
17 
18 #include "CastUniquePointer.h"
19 
21 
24 {
26 
27  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to smooth.");
28  params.addClassDescription("Utilizes the specified smoothing algorithm to attempt to improve "
29  "mesh quality.");
30 
31  MooseEnum SmootherAlgorithm("variational laplace", "variational");
32  params.addParam<MooseEnum>("algorithm", SmootherAlgorithm, "The smoothing algorithm to use.");
33  params.addParam<unsigned int>(
34  "iterations", 1, "Laplace algorithm only: the number of smoothing iterations to do.");
35  params.addRangeCheckedParam<Real>(
36  "dilation_weight",
37  0.5,
38  "dilation_weight >= 0.0 & dilation_weight <= 1.0",
39  "Variational algorithm only: the weight of the dilation metric. The distortion metric is "
40  "given weight 1 - dilation_weight.");
41  params.addParam<bool>("preserve_subdomain_boundaries",
42  true,
43  "Variational algorithm only: whether the input mesh's subdomain boundaries "
44  "should be preserved during the smoothing process.");
45  params.addParam<Real>("relative_residual_tolerance",
46  TOLERANCE * TOLERANCE,
47  "Variational algorithm only: solver relative residual tolerance.");
48  params.addParam<Real>("absolute_residual_tolerance",
49  TOLERANCE * TOLERANCE,
50  "Variational algorithm only: solver absolute residual tolerance.");
51  params.addRangeCheckedParam<unsigned int>(
52  "verbosity",
53  1,
54  "0 <= verbosity <= 100",
55  "Variational algorithm only: verbosity level between 0 and 100.");
56 
57  return params;
58 }
59 
61  : MeshGenerator(parameters),
62  _input(getMesh("input")),
63  _algorithm(getParam<MooseEnum>("algorithm")),
64  _iterations(getParam<unsigned int>("iterations")),
65  _dilation_weight(getParam<Real>("dilation_weight")),
66  _preserve_subdomain_boundaries(getParam<bool>("preserve_subdomain_boundaries")),
67  _relative_residual_tolerance(getParam<Real>("relative_residual_tolerance")),
68  _absolute_residual_tolerance(getParam<Real>("absolute_residual_tolerance")),
69  _verbosity(getParam<unsigned int>("verbosity"))
70 {
71  if (isParamSetByUser("algorithm"))
72  {
73  // Error if user tries to mix laplace smoother params with variational
74  // smoother params
75 
76  std::vector<std::string> check_params;
77  std::string other_algorithm;
78  if (_algorithm == "laplace")
79  {
80  other_algorithm = "variational";
81  check_params = {"dilation_weight",
82  "preserve_subdomain_boundaries",
83  "relative_residual_tolerance",
84  "absolute_residual_tolerance",
85  "verbosity"};
86  }
87 
88  else // _algorithm == "variational"
89  {
90  other_algorithm = "laplace";
91  check_params = {"iterations"};
92  }
93 
94  for (const auto & param_name : check_params)
95  if (isParamSetByUser(param_name))
96  mooseError(" param '",
97  param_name,
98  "' applies to algorithm='",
99  other_algorithm,
100  "' only and has no effect on the ",
101  "currently selected algorithm='",
102  _algorithm,
103  "'.");
104  }
105 }
106 
107 std::unique_ptr<MeshBase>
109 {
110  // This cast transfers ownership from _input to mesh
111  std::unique_ptr<UnstructuredMesh> mesh = dynamic_pointer_cast<UnstructuredMesh>(_input);
112  std::unique_ptr<libMesh::MeshSmoother> smoother = nullptr;
113 
114  if (_algorithm == "laplace")
115  {
116  if (!mesh->is_serial())
117  mooseError(
118  "SmoothMeshGenerator with algorithm='laplace' is not implemented for distributed meshes");
119 
120  smoother = std::make_unique<libMesh::LaplaceMeshSmoother>(*mesh, _iterations);
121  }
122 
123  else if (_algorithm == "variational")
124  {
125  smoother = std::make_unique<libMesh::VariationalMeshSmoother>(*mesh,
130  _verbosity);
131  }
132 
133  smoother->smooth();
134 
135  return dynamic_pointer_cast<MeshBase>(mesh);
136 }
const Real _dilation_weight
Variational only: the dilation weight (variational smoother only)
const Real _relative_residual_tolerance
Variational only: solver relative residual tolerance.
static InputParameters validParams()
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
const unsigned int _verbosity
Variational only: verbosity level between 0 and 100.
const unsigned int _iterations
Laplace only: the number of smoothing passes to do.
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...
std::unique_ptr< MeshBase > & _input
Mesh that possibly comes from another generator.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
registerMooseObject("MooseApp", SmoothMeshGenerator)
static InputParameters validParams()
Definition: MeshGenerator.C:23
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
SmoothMeshGenerator(const InputParameters &parameters)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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:281
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...
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 MooseEnum _algorithm
Smoothing algorithm to use.
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
const Real _absolute_residual_tolerance
Variational only: solver absolute residual tolerance.
MeshGenerator for doing mesh smoothing.
const bool _preserve_subdomain_boundaries
Variational only: whether to preserve subdomain/block boundaries during smoothing.
bool isParamSetByUser(const std::string &name) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
Definition: MooseBase.h:215
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:33
void ErrorVector unsigned int