www.mooseframework.org
CartesianMeshGenerator.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "CartesianMeshGenerator.h"
11 #include "CastUniquePointer.h"
12 
13 // libMesh includes
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"
20 
22 
25 {
27  MooseEnum dims("1=1 2 3");
28  params.addRequiredParam<MooseEnum>("dim", dims, "The dimension of the mesh to be generated");
29 
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)");
33  params.addParam<std::vector<Real>>(
34  "dy", "Intervals in the Y direction (required when dim>1 otherwise ignored)");
35  params.addParam<std::vector<unsigned int>>(
36  "iy", "Number of grids in all intervals in the Y direction (default to all one)");
37  params.addParam<std::vector<Real>>(
38  "dz", "Intervals in the Z direction (required when dim>2 otherwise ignored)");
39  params.addParam<std::vector<unsigned int>>(
40  "iz", "Number of grids in all intervals in the Z direction (default to all one)");
41  params.addParam<std::vector<unsigned int>>("subdomain_id", "Block IDs (default to all zero)");
42  params.addClassDescription("This CartesianMeshGenerator creates a non-uniform Cartesian mesh.");
43  return params;
44 }
45 
47  : MeshGenerator(parameters),
48  _dim(getParam<MooseEnum>("dim")),
49  _dx(getParam<std::vector<Real>>("dx"))
50 {
51  // get all other parameters if provided and check their sizes
52  if (isParamValid("ix"))
53  {
54  _ix = getParam<std::vector<unsigned int>>("ix");
55  if (_ix.size() != _dx.size())
56  mooseError("ix must be in the same size of dx");
57  for (unsigned int i = 0; i < _ix.size(); ++i)
58  if (_ix[i] == 0)
59  mooseError("ix cannot be zero");
60  }
61  else
62  _ix = std::vector<unsigned int>(_dx.size(), 1);
63 
64  for (unsigned int i = 0; i < _dx.size(); ++i)
65  if (_dx[i] <= 0)
66  mooseError("dx must be greater than zero");
67 
68  if (isParamValid("dy"))
69  {
70  _dy = getParam<std::vector<Real>>("dy");
71  for (unsigned int i = 0; i < _dy.size(); ++i)
72  if (_dy[i] <= 0)
73  mooseError("dy must be greater than zero");
74  }
75 
76  if (isParamValid("iy"))
77  {
78  _iy = getParam<std::vector<unsigned int>>("iy");
79  if (_iy.size() != _dy.size())
80  mooseError("iy must be in the same size of dy");
81  for (unsigned int i = 0; i < _iy.size(); ++i)
82  if (_iy[i] == 0)
83  mooseError("iy cannot be zero");
84  }
85  else
86  _iy = std::vector<unsigned int>(_dy.size(), 1);
87 
88  if (isParamValid("dz"))
89  {
90  _dz = getParam<std::vector<Real>>("dz");
91  for (unsigned int i = 0; i < _dz.size(); ++i)
92  if (_dz[i] <= 0)
93  mooseError("dz must be greater than zero");
94  }
95 
96  if (isParamValid("iz"))
97  {
98  _iz = getParam<std::vector<unsigned int>>("iz");
99  if (_iz.size() != _dz.size())
100  mooseError("iz must be in the same size of dz");
101  for (unsigned int i = 0; i < _iz.size(); ++i)
102  if (_iz[i] == 0)
103  mooseError("iz cannot be zero");
104  }
105  else
106  _iz = std::vector<unsigned int>(_dz.size(), 1);
107 
108  if (isParamValid("subdomain_id"))
109  {
110  _subdomain_id = getParam<std::vector<unsigned int>>("subdomain_id");
111  if (isParamValid("dz") && isParamValid("dy"))
112  {
113  if (_subdomain_id.size() != _dx.size() * _dy.size() * _dz.size())
114  mooseError("subdomain_id must be in the size of product of sizes of dx, dy and dz");
115  }
116  else if (isParamValid("dy"))
117  {
118  if (_subdomain_id.size() != _dx.size() * _dy.size())
119  mooseError("subdomain_id must be in the size of product of sizes of dx and dy");
120  }
121  else
122  {
123  if (_subdomain_id.size() != _dx.size())
124  mooseError("subdomain_id must be in the size of product of sizes of dx");
125  }
126  }
127  else
128  {
129  if (isParamValid("dz"))
130  _subdomain_id = std::vector<unsigned int>(_dx.size() * _dy.size() * _dz.size(), 0);
131  else if (isParamValid("dy"))
132  _subdomain_id = std::vector<unsigned int>(_dx.size() * _dy.size(), 0);
133  else
134  _subdomain_id = std::vector<unsigned int>(_dx.size(), 0);
135  }
136 
137  // do dimension checks and expand block IDs for all sub-grids
138  switch (_dim)
139  {
140  case 1:
141  {
142  _nx = 0;
143  for (unsigned int i = 0; i < _dx.size(); ++i)
144  _nx += _ix[i];
145  _ny = 1;
146  _nz = 1;
147 
148  std::vector<unsigned int> new_id;
149  for (unsigned int i = 0; i < _dx.size(); ++i)
150  for (unsigned int ii = 0; ii < _ix[i]; ++ii)
151  new_id.push_back(_subdomain_id[i]);
152 
153  _subdomain_id = new_id;
154 
155  if (isParamValid("dy"))
156  mooseWarning("dy provided for 1D");
157  if (isParamValid("iy"))
158  mooseWarning("iy provided for 1D");
159  if (isParamValid("dz"))
160  mooseWarning("dz provided for 1D");
161  if (isParamValid("iz"))
162  mooseWarning("iz provided for 1D");
163  break;
164  }
165  case 2:
166  {
167  _nx = 0;
168  for (unsigned int i = 0; i < _dx.size(); ++i)
169  _nx += _ix[i];
170  _ny = 0;
171  for (unsigned int i = 0; i < _dy.size(); ++i)
172  _ny += _iy[i];
173  _nz = 1;
174 
175  std::vector<unsigned int> new_id;
176  for (unsigned int j = 0; j < _dy.size(); ++j)
177  for (unsigned int jj = 0; jj < _iy[j]; ++jj)
178  for (unsigned int i = 0; i < _dx.size(); ++i)
179  for (unsigned int ii = 0; ii < _ix[i]; ++ii)
180  new_id.push_back(_subdomain_id[j * _dx.size() + i]);
181 
182  _subdomain_id = new_id;
183 
184  if (!isParamValid("dy"))
185  mooseError("dy is not provided for 2D");
186  if (isParamValid("dz"))
187  mooseWarning("dz provided for 2D");
188  if (isParamValid("iz"))
189  mooseWarning("iz provided for 2D");
190  break;
191  }
192  case 3:
193  {
194  _nx = 0;
195  for (unsigned int i = 0; i < _dx.size(); ++i)
196  _nx += _ix[i];
197  _ny = 0;
198  for (unsigned int i = 0; i < _dy.size(); ++i)
199  _ny += _iy[i];
200  _nz = 0;
201  for (unsigned int i = 0; i < _dz.size(); ++i)
202  _nz += _iz[i];
203 
204  std::vector<unsigned int> new_id;
205  for (unsigned int k = 0; k < _dz.size(); ++k)
206  for (unsigned int kk = 0; kk < _iz[k]; ++kk)
207  for (unsigned int j = 0; j < _dy.size(); ++j)
208  for (unsigned int jj = 0; jj < _iy[j]; ++jj)
209  for (unsigned int i = 0; i < _dx.size(); ++i)
210  for (unsigned int ii = 0; ii < _ix[i]; ++ii)
211  new_id.push_back(_subdomain_id[k * _dx.size() * _dy.size() + j * _dx.size() + i]);
212 
213  _subdomain_id = new_id;
214 
215  if (!isParamValid("dy"))
216  mooseError("dy is not provided for 3D");
217  if (!isParamValid("dz"))
218  mooseError("dz is not provided for 3D");
219  break;
220  }
221  }
222 }
223 
224 std::unique_ptr<MeshBase>
226 {
227  auto mesh = buildMeshBaseObject();
228 
229  // switching on MooseEnum to generate the reference mesh
230  // Note: element type is fixed
231  switch (_dim)
232  {
233  case 1:
234  MeshTools::Generation::build_line(static_cast<UnstructuredMesh &>(*mesh), _nx, 0, _nx, EDGE2);
235  break;
236  case 2:
237  MeshTools::Generation::build_square(
238  static_cast<UnstructuredMesh &>(*mesh), _nx, _ny, 0, _nx, 0, _ny, QUAD4);
239  break;
240  case 3:
241  MeshTools::Generation::build_cube(
242  static_cast<UnstructuredMesh &>(*mesh), _nx, _ny, _nz, 0, _nx, 0, _ny, 0, _nz, HEX8);
243  break;
244  }
245 
246  // assign block IDs
247  MeshBase::element_iterator el = mesh->active_elements_begin();
248  MeshBase::element_iterator el_end = mesh->active_elements_end();
249  for (; el != el_end; ++el)
250  {
251  const Point p = (*el)->vertex_average();
252  unsigned int ix = std::floor(p(0));
253  unsigned int iy = std::floor(p(1));
254  unsigned int iz = std::floor(p(2));
255  unsigned int i = iz * _nx * _ny + iy * _nx + ix;
256  (*el)->subdomain_id() = _subdomain_id[i];
257  }
258 
259  // adjust node coordinates
260  switch (_dim)
261  {
262  case 1:
263  {
264  Real base;
265 
266  std::vector<Real> mapx;
267  // Note: the starting coordinate is zero
268  base = 0;
269  mapx.push_back(base);
270  for (unsigned int i = 0; i < _dx.size(); ++i)
271  {
272  for (unsigned int j = 1; j <= _ix[i]; ++j)
273  mapx.push_back(base + _dx[i] / _ix[i] * j);
274  base += _dx[i];
275  }
276 
277  MeshBase::node_iterator node = mesh->active_nodes_begin();
278  MeshBase::node_iterator node_end = mesh->active_nodes_end();
279  for (; node != node_end; ++node)
280  {
281  unsigned int i = (*(*node))(0) + 0.5;
282  (*(*node))(0) = mapx.at(i);
283  }
284  break;
285  }
286  case 2:
287  {
288  Real base;
289 
290  std::vector<Real> mapx;
291  base = 0;
292  mapx.push_back(base);
293  for (unsigned int i = 0; i < _dx.size(); ++i)
294  {
295  for (unsigned int j = 1; j <= _ix[i]; ++j)
296  mapx.push_back(base + _dx[i] / _ix[i] * j);
297  base += _dx[i];
298  }
299 
300  std::vector<Real> mapy;
301  base = 0;
302  mapy.push_back(base);
303  for (unsigned int i = 0; i < _dy.size(); ++i)
304  {
305  for (unsigned int j = 1; j <= _iy[i]; ++j)
306  mapy.push_back(base + _dy[i] / _iy[i] * j);
307  base += _dy[i];
308  }
309 
310  MeshBase::node_iterator node = mesh->active_nodes_begin();
311  MeshBase::node_iterator node_end = mesh->active_nodes_end();
312  for (; node != node_end; ++node)
313  {
314  unsigned int i = (*(*node))(0) + 0.5;
315  (*(*node))(0) = mapx.at(i);
316  unsigned int j = (*(*node))(1) + 0.5;
317  (*(*node))(1) = mapy.at(j);
318  }
319  break;
320  }
321  case 3:
322  {
323  Real base;
324 
325  std::vector<Real> mapx;
326  base = 0;
327  mapx.push_back(base);
328  for (unsigned int i = 0; i < _dx.size(); ++i)
329  {
330  for (unsigned int j = 1; j <= _ix[i]; ++j)
331  mapx.push_back(base + _dx[i] / _ix[i] * j);
332  base += _dx[i];
333  }
334 
335  std::vector<Real> mapy;
336  base = 0;
337  mapy.push_back(base);
338  for (unsigned int i = 0; i < _dy.size(); ++i)
339  {
340  for (unsigned int j = 1; j <= _iy[i]; ++j)
341  mapy.push_back(base + _dy[i] / _iy[i] * j);
342  base += _dy[i];
343  }
344 
345  std::vector<Real> mapz;
346  base = 0;
347  mapz.push_back(base);
348  for (unsigned int i = 0; i < _dz.size(); ++i)
349  {
350  for (unsigned int j = 1; j <= _iz[i]; ++j)
351  mapz.push_back(base + _dz[i] / _iz[i] * j);
352  base += _dz[i];
353  }
354 
355  MeshBase::node_iterator node = mesh->active_nodes_begin();
356  MeshBase::node_iterator node_end = mesh->active_nodes_end();
357  for (; node != node_end; ++node)
358  {
359  unsigned int i = (*(*node))(0) + 0.5;
360  (*(*node))(0) = mapx.at(i);
361  unsigned int j = (*(*node))(1) + 0.5;
362  (*(*node))(1) = mapy.at(j);
363  unsigned int k = (*(*node))(2) + 0.5;
364  (*(*node))(2) = mapz.at(k);
365  }
366  break;
367  }
368  }
369 
370  mesh->prepare_for_use();
371  return dynamic_pointer_cast<MeshBase>(mesh);
372 }
MooseEnum _dim
The dimension of the mesh.
HEX8
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
CartesianMeshGenerator(const InputParameters &parameters)
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.
int _nx
Number of elements in x, y, z direction.
void mooseWarning(Args &&... args) const
Emits a warning prefixed with object name and type.
std::vector< Real > _dy
Intervals in y direction.
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...
QUAD4
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
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...
Definition: MooseEnum.h:31
std::vector< unsigned int > _iy
Number of grids in all intervals in y direction.
static InputParameters validParams()
Definition: MeshGenerator.C:23
std::vector< Real > _dx
Intervals in x direction.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
EDGE2
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.
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 option parameter and a documentation string to the InputParameters object...
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.
Definition: MeshGenerator.h:32
static InputParameters validParams()