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