www.mooseframework.org
PolycrystalHex.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
7 
8 #include "PolycrystalHex.h"
9 #include "MooseRandom.h"
10 #include "MooseMesh.h"
11 #include "MathUtils.h"
12 
13 template <>
14 InputParameters
16 {
17  InputParameters params = validParams<PolycrystalVoronoi>();
18  params.addClassDescription("Perturbed hexagonal polycrystal");
19  params.addParam<Real>("x_offset", 0.5, "Specifies offset of hexagon grid in x-direction");
20  params.addRangeCheckedParam<Real>(
21  "perturbation_percent",
22  0.0,
23  "perturbation_percent >= 0.0 & perturbation_percent <= 1.0",
24  "The percent to randomly perturb centers of grains relative to the size of the grain");
25  return params;
26 }
27 
28 PolycrystalHex::PolycrystalHex(const InputParameters & parameters)
29  : PolycrystalVoronoi(parameters),
30  _x_offset(getParam<Real>("x_offset")),
31  _perturbation_percent(getParam<Real>("perturbation_percent"))
32 {
33  _random.seed(_tid, getParam<unsigned int>("rand_seed"));
34 }
35 
36 void
38 {
39  const unsigned int root = MathUtils::round(std::pow(_grain_num, 1.0 / _dim));
40 
41  // integer power the rounded root and check if we recover the grain number
42  unsigned int grain_pow = root;
43  for (unsigned int i = 1; i < _dim; ++i)
44  grain_pow *= root;
45 
46  if (_grain_num != grain_pow)
47  mooseError("PolycrystalHex requires a square or cubic number depending on the mesh dimension");
48 
49  // Set up domain bounds with mesh tools
50  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
51  {
52  _bottom_left(i) = _mesh.getMinInDimension(i);
53  _top_right(i) = _mesh.getMaxInDimension(i);
54  }
56 
57  _centerpoints.resize(_grain_num);
58 
59  std::vector<Real> distances(_grain_num);
60  std::vector<Point> holder(_grain_num);
61 
62  const Real ndist = 1.0 / root;
63 
64  // Assign the relative center points positions, defining the grains according to a hexagonal
65  // pattern
66  unsigned int count = 0;
67  for (unsigned int k = 0; k < (_dim == 3 ? root : 1); ++k)
68  for (unsigned int j = 0; j < (_dim >= 2 ? root : 1); ++j)
69  for (unsigned int i = 0; i < root; ++i)
70  {
71  // set x-coordinate
72  holder[count](0) = i * ndist + (0.5 * ndist * (j % 2)) + _x_offset * ndist;
73 
74  // set y-coordinate
75  holder[count](1) = j * ndist + (0.5 * ndist * (k % 2));
76 
77  // set z-coordinate
78  holder[count](2) = k * ndist;
79 
80  // increment counter
81  count++;
82  }
83 
84  // Assign center point values
85  for (unsigned int grain = 0; grain < _grain_num; ++grain)
86  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
87  {
88  if (_range(i) == 0)
89  continue;
90 
91  Real perturbation_dist = (_range(i) / root * (_random.rand(_tid) * 2 - 1.0)) *
92  _perturbation_percent; // Perturb -100 to 100%
93  _centerpoints[grain](i) = _bottom_left(i) + _range(i) * holder[grain](i) + perturbation_dist;
94 
95  if (_centerpoints[grain](i) > _top_right(i))
96  _centerpoints[grain](i) = _top_right(i);
97  if (_centerpoints[grain](i) < _bottom_left(i))
98  _centerpoints[grain](i) = _bottom_left(i);
99  }
100 }
const unsigned int _grain_num
The number of grains to create.
MooseRandom _random
InputParameters validParams< PolycrystalHex >()
Real round(Real x)
Definition: MathUtils.h:18
std::vector< Point > _centerpoints
const Real _perturbation_percent
InputParameters validParams< PolycrystalVoronoi >()
virtual void precomputeGrainStructure()
This callback is triggered after the object is initialized and may be optionally overridden to do pre...
PolycrystalHex(const InputParameters &parameters)
const unsigned int _dim
mesh dimension
Real root(std::function< Real(Real)> const &f, Real x1, Real x2, Real tol=1.0e-12)
Finds the root of a function using Brent&#39;s method.
Definition: BrentsMethod.C:58
ExpressionBuilder::EBTerm pow(const ExpressionBuilder::EBTerm &left, T exponent)
const Real _x_offset
MooseMesh & _mesh
A reference to the mesh.