www.mooseframework.org
PolycrystalVoronoi.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 "PolycrystalVoronoi.h"
9 #include "IndirectSort.h"
10 #include "MooseRandom.h"
11 #include "MooseMesh.h"
12 #include "MooseVariable.h"
13 #include "NonlinearSystemBase.h"
14 
15 template <>
16 InputParameters
18 {
19  InputParameters params = validParams<PolycrystalUserObjectBase>();
20  params.addClassDescription(
21  "Random Voronoi tesselation polycrystal (used by PolycrystalVoronoiAction)");
22  params.addRequiredParam<unsigned int>(
23  "grain_num", "Number of grains being represented by the order parameters");
24 
25  params.addParam<unsigned int>("rand_seed", 0, "The random seed");
26  params.addParam<bool>(
27  "columnar_3D", false, "3D microstructure will be columnar in the z-direction?");
28 
29  return params;
30 }
31 
32 PolycrystalVoronoi::PolycrystalVoronoi(const InputParameters & parameters)
33  : PolycrystalUserObjectBase(parameters),
34  _grain_num(getParam<unsigned int>("grain_num")),
35  _columnar_3D(getParam<bool>("columnar_3D")),
36  _rand_seed(getParam<unsigned int>("rand_seed"))
37 {
38 }
39 
40 void
42  std::vector<unsigned int> & grains) const
43 {
44  auto n_grains = _centerpoints.size();
45  auto min_distance = _range.norm();
46  auto min_index = n_grains;
47 
48  // Loops through all of the grain centers and finds the center that is closest to the point p
49  for (auto grain = beginIndex(_centerpoints); grain < n_grains; ++grain)
50  {
51  auto distance = _mesh.minPeriodicDistance(_vars[0]->number(), _centerpoints[grain], point);
52 
53  if (distance < min_distance)
54  {
55  min_distance = distance;
56  min_index = grain;
57  }
58  }
59 
60  mooseAssert(min_index < n_grains, "Couldn't find closest Voronoi cell");
61 
62  grains.resize(1);
63  grains[0] = min_index;
64 }
65 
66 Real
67 PolycrystalVoronoi::getVariableValue(unsigned int op_index, const Point & p) const
68 {
69  std::vector<unsigned int> grain_ids;
70  getGrainsBasedOnPoint(p, grain_ids);
71 
72  // Now see if any of those grains are represented by the passed in order parameter
73  unsigned int active_grain_on_op = invalid_id;
74  for (auto grain_id : grain_ids)
75  if (op_index == _grain_to_op[grain_id])
76  {
77  active_grain_on_op = grain_id;
78  break;
79  }
80 
81  return active_grain_on_op != invalid_id ? 1.0 : 0.0;
82 }
83 
84 void
86 {
87  MooseRandom::seed(_rand_seed);
88 
89  // Set up domain bounds with mesh tools
90  for (unsigned int i = 0; i < LIBMESH_DIM; i++)
91  {
92  _bottom_left(i) = _mesh.getMinInDimension(i);
93  _top_right(i) = _mesh.getMaxInDimension(i);
94  }
96 
97  // Randomly generate the centers of the individual grains represented by the Voronoi tessellation
98  _centerpoints.resize(_grain_num);
99  std::vector<Real> distances(_grain_num);
100 
101  for (auto grain = decltype(_grain_num)(0); grain < _grain_num; grain++)
102  {
103  for (unsigned int i = 0; i < LIBMESH_DIM; i++)
104  _centerpoints[grain](i) = _bottom_left(i) + _range(i) * MooseRandom::rand();
105  if (_columnar_3D)
106  _centerpoints[grain](2) = _bottom_left(2) + _range(2) * 0.5;
107  }
108 }
const unsigned int _grain_num
The number of grains to create.
virtual Real getVariableValue(unsigned int op_index, const Point &p) const override
Returns the variable value for a given op_index and mesh point.
std::vector< Point > _centerpoints
This object provides the base capability for creating proper polycrystal ICs.
std::vector< unsigned int > _grain_to_op
A vector indicating which op is assigned to each grain.
std::vector< MooseVariable * > _vars
The vector of coupled in variables.
PolycrystalVoronoi(const InputParameters &parameters)
static const unsigned int invalid_id
InputParameters validParams< PolycrystalUserObjectBase >()
virtual void getGrainsBasedOnPoint(const Point &point, std::vector< unsigned int > &grains) const override
Method for retrieving active grain IDs based on some point in the mesh.
virtual void precomputeGrainStructure() override
This callback is triggered after the object is initialized and may be optionally overridden to do pre...
InputParameters validParams< PolycrystalVoronoi >()
const unsigned int _rand_seed
MooseMesh & _mesh
A reference to the mesh.