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