14 #include "libmesh/mesh_generation.h" 15 #include "libmesh/unstructured_mesh.h" 16 #include "libmesh/replicated_mesh.h" 17 #include "libmesh/point.h" 18 #include "libmesh/elem.h" 19 #include "libmesh/node.h" 30 params.
addRequiredParam<std::vector<Real>>(
"dx",
"Intervals in the X direction");
31 params.
addParam<std::vector<unsigned int>>(
32 "ix",
"Number of grids in all intervals in the X direction (default to all one)");
35 "Maximum element size in the X direction. This can be specified once for all 'dx' intervals " 36 "or specified for each interval. 'ix' is then determined using the ceiling of dx/size");
38 "dy",
"Intervals in the Y direction (required when dim>1 otherwise ignored)");
39 params.
addParam<std::vector<unsigned int>>(
40 "iy",
"Number of grids in all intervals in the Y direction (default to all one)");
43 "Maximum element size in the Y direction. This can be specified once for all 'dy' intervals " 44 "or specified for each interval. 'iy' is then determined using the ceiling of dy/size");
46 "dz",
"Intervals in the Z direction (required when dim>2 otherwise ignored)");
47 params.
addParam<std::vector<unsigned int>>(
48 "iz",
"Number of grids in all intervals in the Z direction (default to all one)");
51 "Maximum element size in the Z direction. This can be specified once for all 'dz' intervals " 52 "or specified for each interval. 'iz' is then determined using the ceiling of dz/size");
53 params.
addParam<std::vector<unsigned int>>(
"subdomain_id",
"Block IDs (default to all zero)");
54 params.
addClassDescription(
"This CartesianMeshGenerator creates a non-uniform Cartesian mesh.");
61 _dx(getParam<
std::vector<
Real>>(
"dx"))
66 _ix = getParam<std::vector<unsigned int>>(
"ix");
67 if (
_ix.size() !=
_dx.size())
69 for (
unsigned int i = 0; i <
_ix.size(); ++i)
73 paramError(
"max_sizes_in_x",
"Cannot be provided if 'ix' is specified");
78 auto max_sizes = getParam<std::vector<Real>>(
"max_sizes_in_x");
79 if (max_sizes.size() == 1)
80 max_sizes.resize(
_dx.size(), max_sizes[0]);
81 if (max_sizes.size() !=
_dx.size())
82 paramError(
"max_sizes_in_x",
"max_sizes_in_x must be size 1 or the same size as dx");
84 if (max_sizes[i] <= 0)
85 paramError(
"max_sizes_in_x",
"max_sizes_in_x must be strictly positive.");
87 _ix[i] = std::ceil(
_dx[i] / max_sizes[i]);
90 _ix = std::vector<unsigned int>(
_dx.size(), 1);
92 for (
unsigned int i = 0; i <
_dx.size(); ++i)
98 _dy = getParam<std::vector<Real>>(
"dy");
99 for (
unsigned int i = 0; i <
_dy.size(); ++i)
106 _iy = getParam<std::vector<unsigned int>>(
"iy");
107 if (
_iy.size() !=
_dy.size())
109 for (
unsigned int i = 0; i <
_iy.size(); ++i)
113 paramError(
"max_sizes_in_y",
"Cannot be provided if 'iy' is specified");
118 auto max_sizes = getParam<std::vector<Real>>(
"max_sizes_in_y");
119 if (max_sizes.size() == 1)
120 max_sizes.resize(
_dy.size(), max_sizes[0]);
121 if (max_sizes.size() !=
_dy.size())
122 paramError(
"max_sizes_in_y",
"max_sizes_in_y must be size 1 or the same size as dy");
124 if (max_sizes[i] <= 0)
125 paramError(
"max_sizes_in_y",
"max_sizes_in_y must be strictly positive.");
127 _iy[i] = std::ceil(
_dy[i] / max_sizes[i]);
130 _iy = std::vector<unsigned int>(
_dy.size(), 1);
134 _dz = getParam<std::vector<Real>>(
"dz");
135 for (
unsigned int i = 0; i <
_dz.size(); ++i)
142 _iz = getParam<std::vector<unsigned int>>(
"iz");
143 if (
_iz.size() !=
_dz.size())
145 for (
unsigned int i = 0; i <
_iz.size(); ++i)
149 paramError(
"max_sizes_in_z",
"Cannot be provided if 'iz' is specified");
154 auto max_sizes = getParam<std::vector<Real>>(
"max_sizes_in_z");
155 if (max_sizes.size() == 1)
156 max_sizes.resize(
_dz.size(), max_sizes[0]);
157 if (max_sizes.size() !=
_dz.size())
158 paramError(
"max_sizes_in_z",
"max_sizes_in_z must be size 1 or the same size as dz");
160 if (max_sizes[i] <= 0)
161 paramError(
"max_sizes_in_z",
"max_sizes_in_z must be strictly positive.");
163 _iz[i] = std::ceil(
_dz[i] / max_sizes[i]);
166 _iz = std::vector<unsigned int>(
_dz.size(), 1);
170 _subdomain_id = getParam<std::vector<unsigned int>>(
"subdomain_id");
175 "subdomain_id must be the size of the product of sizes of dx, dy and dz. Sizes are '" +
177 std::to_string(
_dx.size() *
_dy.size() *
_dz.size()) +
"' respectively");
183 "subdomain_id must be the size of the product of sizes of dx and dy. Sizes are '" +
185 std::to_string(
_dx.size() *
_dy.size()) +
"' respectively");
190 mooseError(
"subdomain_id must be the size of the product of sizes of dx. Sizes are '" +
191 std::to_string(
_subdomain_id.size()) +
"' and '" + std::to_string(
_dx.size()) +
211 for (
unsigned int i = 0; i <
_dx.size(); ++i)
216 std::vector<unsigned int> new_id;
217 for (
unsigned int i = 0; i <
_dx.size(); ++i)
218 for (
unsigned int ii = 0; ii <
_ix[i]; ++ii)
232 paramWarning(
"max_sizes_in_y",
"Should not be provided for 1D");
234 paramWarning(
"max_sizes_in_z",
"Should not be provided for 1D");
240 for (
unsigned int i = 0; i <
_dx.size(); ++i)
243 for (
unsigned int i = 0; i <
_dy.size(); ++i)
247 std::vector<unsigned int> new_id;
248 for (
unsigned int j = 0; j <
_dy.size(); ++j)
249 for (
unsigned int jj = 0; jj <
_iy[j]; ++jj)
250 for (
unsigned int i = 0; i <
_dx.size(); ++i)
251 for (
unsigned int ii = 0; ii <
_ix[i]; ++ii)
263 paramWarning(
"max_sizes_in_z",
"Should not be provided for 2D");
269 for (
unsigned int i = 0; i <
_dx.size(); ++i)
272 for (
unsigned int i = 0; i <
_dy.size(); ++i)
275 for (
unsigned int i = 0; i <
_dz.size(); ++i)
278 std::vector<unsigned int> new_id;
279 for (
unsigned int k = 0; k <
_dz.size(); ++k)
280 for (
unsigned int kk = 0; kk <
_iz[k]; ++kk)
281 for (
unsigned int j = 0; j <
_dy.size(); ++j)
282 for (
unsigned int jj = 0; jj <
_iy[j]; ++jj)
283 for (
unsigned int i = 0; i <
_dx.size(); ++i)
284 for (
unsigned int ii = 0; ii <
_ix[i]; ++ii)
298 std::unique_ptr<MeshBase>
308 MeshTools::Generation::build_line(static_cast<UnstructuredMesh &>(*
mesh),
_nx, 0,
_nx,
EDGE2);
311 MeshTools::Generation::build_square(
315 MeshTools::Generation::build_cube(
316 static_cast<UnstructuredMesh &>(*
mesh),
_nx,
_ny,
_nz, 0,
_nx, 0,
_ny, 0,
_nz,
HEX8);
321 MeshBase::element_iterator el =
mesh->active_elements_begin();
322 MeshBase::element_iterator el_end =
mesh->active_elements_end();
323 for (; el != el_end; ++el)
325 const Point p = (*el)->vertex_average();
326 unsigned int ix = std::floor(p(0));
327 unsigned int iy = std::floor(p(1));
328 unsigned int iz = std::floor(p(2));
329 unsigned int i = iz *
_nx *
_ny + iy *
_nx + ix;
340 std::vector<Real> mapx;
343 mapx.push_back(base);
344 for (
unsigned int i = 0; i <
_dx.size(); ++i)
346 for (
unsigned int j = 1; j <=
_ix[i]; ++j)
347 mapx.push_back(base +
_dx[i] /
_ix[i] * j);
351 MeshBase::node_iterator node =
mesh->active_nodes_begin();
352 MeshBase::node_iterator node_end =
mesh->active_nodes_end();
353 for (; node != node_end; ++node)
355 unsigned int i = (*(*node))(0) + 0.5;
356 (*(*node))(0) = mapx.at(i);
364 std::vector<Real> mapx;
366 mapx.push_back(base);
367 for (
unsigned int i = 0; i <
_dx.size(); ++i)
369 for (
unsigned int j = 1; j <=
_ix[i]; ++j)
370 mapx.push_back(base +
_dx[i] /
_ix[i] * j);
374 std::vector<Real> mapy;
376 mapy.push_back(base);
377 for (
unsigned int i = 0; i <
_dy.size(); ++i)
379 for (
unsigned int j = 1; j <=
_iy[i]; ++j)
380 mapy.push_back(base +
_dy[i] /
_iy[i] * j);
384 MeshBase::node_iterator node =
mesh->active_nodes_begin();
385 MeshBase::node_iterator node_end =
mesh->active_nodes_end();
386 for (; node != node_end; ++node)
388 unsigned int i = (*(*node))(0) + 0.5;
389 (*(*node))(0) = mapx.at(i);
390 unsigned int j = (*(*node))(1) + 0.5;
391 (*(*node))(1) = mapy.at(j);
399 std::vector<Real> mapx;
401 mapx.push_back(base);
402 for (
unsigned int i = 0; i <
_dx.size(); ++i)
404 for (
unsigned int j = 1; j <=
_ix[i]; ++j)
405 mapx.push_back(base +
_dx[i] /
_ix[i] * j);
409 std::vector<Real> mapy;
411 mapy.push_back(base);
412 for (
unsigned int i = 0; i <
_dy.size(); ++i)
414 for (
unsigned int j = 1; j <=
_iy[i]; ++j)
415 mapy.push_back(base +
_dy[i] /
_iy[i] * j);
419 std::vector<Real> mapz;
421 mapz.push_back(base);
422 for (
unsigned int i = 0; i <
_dz.size(); ++i)
424 for (
unsigned int j = 1; j <=
_iz[i]; ++j)
425 mapz.push_back(base +
_dz[i] /
_iz[i] * j);
429 MeshBase::node_iterator node =
mesh->active_nodes_begin();
430 MeshBase::node_iterator node_end =
mesh->active_nodes_end();
431 for (; node != node_end; ++node)
433 unsigned int i = (*(*node))(0) + 0.5;
434 (*(*node))(0) = mapx.at(i);
435 unsigned int j = (*(*node))(1) + 0.5;
436 (*(*node))(1) = mapy.at(j);
437 unsigned int k = (*(*node))(2) + 0.5;
438 (*(*node))(2) = mapz.at(k);
444 mesh->prepare_for_use();
MooseEnum _dim
The dimension of the mesh.
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
CartesianMeshGenerator(const InputParameters ¶meters)
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.
int _nx
Number of elements in x, y, z direction.
std::vector< Real > _dy
Intervals in y direction.
void mooseWarning(Args &&... args) const
std::vector< unsigned int > _ix
Number of grids in all intervals in x direction.
std::vector< unsigned int > _subdomain_id
Block IDs.
registerMooseObject("MooseApp", CartesianMeshGenerator)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
std::vector< unsigned int > _iy
Number of grids in all intervals in y direction.
static InputParameters validParams()
std::string stringify(const T &t)
conversion to string
std::vector< Real > _dx
Intervals in x direction.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< unsigned int > _iz
Number of grids in all intervals in z direction.
std::vector< Real > _dz
Intervals in z direction.
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
void paramWarning(const std::string ¶m, Args... args) const
std::unique_ptr< MeshBase > buildMeshBaseObject(unsigned int dim=libMesh::invalid_uint)
Build a MeshBase object whose underlying type will be determined by the Mesh input file block...
MeshGenerators are objects that can modify or add to an existing mesh.
auto index_range(const T &sizable)
static InputParameters validParams()