www.mooseframework.org
PolycrystalCircles.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 "PolycrystalCircles.h"
9 #include "MooseMesh.h"
10 #include "MooseVariable.h"
11 
12 #include <fstream>
13 
14 template <>
15 InputParameters
17 {
18  InputParameters params = validParams<PolycrystalUserObjectBase>();
19  params.addClassDescription(
20  "Polycrystal circles generated from a vector input or read from a file");
21  params.addParam<bool>("read_from_file",
22  false,
23  "Set to true to read the position and radius "
24  "vectors from a file rather than inputing them "
25  "manually");
26  params.addParam<bool>(
27  "columnar_3D", false, "3D microstructure will be columnar in the z-direction?");
28  params.addParam<std::vector<Real>>("x_positions", "x coordinate for each circle center");
29  params.addParam<std::vector<Real>>("y_positions", "y coordinate for each circle center");
30  params.addParam<std::vector<Real>>("z_positions", "z coordinate for each circle center");
31  params.addParam<std::vector<Real>>("radii", "The radius for each circle");
32  params.addParam<FileName>("file_name", "File containing circle centers and radii");
33 
34  return params;
35 }
36 
37 PolycrystalCircles::PolycrystalCircles(const InputParameters & parameters)
38  : PolycrystalUserObjectBase(parameters),
39  _columnar_3D(getParam<bool>("columnar_3D")),
40  _grain_num(0)
41 {
42 }
43 
44 void
46  std::vector<unsigned int> & grains) const
47 {
48  unsigned int n_grains = _centerpoints.size();
49  grains.resize(0);
50 
51  for (unsigned int i = 0; i < n_grains; ++i)
52  {
53  Real distance = 0;
54 
55  if (_columnar_3D)
56  {
57  Real d_x = (point(0) - _centerpoints[i](0)) * (point(0) - _centerpoints[i](0));
58  Real d_y = (point(1) - _centerpoints[i](1)) * (point(1) - _centerpoints[i](1));
59  distance = std::sqrt(d_x + d_y);
60  }
61  else
62  distance = _mesh.minPeriodicDistance(_vars[0]->number(), _centerpoints[i], point);
63 
64  if (distance < _radii[i])
65  grains.push_back(i);
66  }
67 }
68 
69 Real
70 PolycrystalCircles::getVariableValue(unsigned int op_index, const Point & p) const
71 {
72  std::vector<unsigned int> grain_ids;
73  getGrainsBasedOnPoint(p, grain_ids);
74 
75  unsigned int active_grain_on_op = invalid_id;
76  for (auto grain_id : grain_ids)
77  if (op_index == _grain_to_op[grain_id])
78  {
79  active_grain_on_op = grain_id;
80  break;
81  }
82 
83  return active_grain_on_op != invalid_id ? 1.0 : 0.0;
84 }
85 
86 void
88 {
89  bool readfromfile = getParam<bool>("read_from_file");
90  if (readfromfile)
91  {
92  // Read file
93  const FileName file_name = getParam<FileName>("file_name");
94  MooseUtils::DelimitedFileReader txt_reader(file_name, &_communicator);
95 
96  txt_reader.read();
97  std::vector<std::string> col_names = txt_reader.getNames();
98  std::vector<std::vector<Real>> data = txt_reader.getData();
99  _grain_num = data[0].size();
100  _centerpoints.resize(_grain_num);
101 
102  std::array<int, 4> col_map = {{-1, -1, -1, -1}};
103 
104  for (unsigned int i = 0; i < col_names.size(); ++i)
105  {
106  // Check vector lengths
107  if (data[i].size() != _grain_num)
108  mooseError("Columns in ", file_name, " do not have uniform lengths.");
109 
110  // Map columns to variables
111  if (col_names[i] == "x")
112  col_map[X] = i;
113  else if (col_names[i] == "y")
114  col_map[Y] = i;
115  else if (col_names[i] == "z")
116  col_map[Z] = i;
117  else if (col_names[i] == "r")
118  col_map[R] = i;
119  }
120 
121  // Check all columns are included
122  if (col_map[X] == -1)
123  mooseError("No column 'x' in ", file_name, ".");
124  if (col_map[Y] == -1)
125  mooseError("No column 'y' in ", file_name, ".");
126  if (col_map[Z] == -1)
127  mooseError("No column 'z' in ", file_name, ".");
128  if (col_map[R] == -1)
129  mooseError("No column 'r' in ", file_name, ".");
130 
131  // Write data to variables
132  _radii.assign(data[col_map[R]].begin(), data[col_map[R]].end());
133  for (unsigned int i = 0; i < _grain_num; ++i)
134  {
135  _centerpoints[i](0) = data[col_map[X]][i];
136  _centerpoints[i](1) = data[col_map[Y]][i];
137  _centerpoints[i](2) = data[col_map[Z]][i];
138  }
139  }
140  else // if (readfromfile)
141  {
142  // Read vectors
143  std::vector<Real> x_c = getParam<std::vector<Real>>("x_positions");
144  std::vector<Real> y_c = getParam<std::vector<Real>>("y_positions");
145  std::vector<Real> z_c = getParam<std::vector<Real>>("z_positions");
146  std::vector<Real> r_c = getParam<std::vector<Real>>("radii");
147 
148  _grain_num = r_c.size();
149  _centerpoints.resize(_grain_num);
150 
151  // Check vector lengths
152  if (_grain_num != x_c.size())
153  mooseError("The vector length of x_positions does not match the length of radii");
154  else if (_grain_num != y_c.size())
155  mooseError("The vector length of y_positions does not match the length of radii");
156  else if (_grain_num != z_c.size())
157  mooseError("The vector length of z_positions does not match the length of radii");
158 
159  // Assign values
160  _radii.assign(r_c.begin(), r_c.end());
161  for (unsigned int i = 0; i < _grain_num; ++i)
162  {
163  _centerpoints[i](0) = x_c[i];
164  _centerpoints[i](1) = y_c[i];
165  _centerpoints[i](2) = z_c[i];
166  }
167  }
168 }
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.
This object provides the base capability for creating proper polycrystal ICs.
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< Real > _radii
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.
static const unsigned int invalid_id
InputParameters validParams< PolycrystalCircles >()
PolycrystalCircles(const InputParameters &parameters)
InputParameters validParams< PolycrystalUserObjectBase >()
virtual void precomputeGrainStructure() override
This callback is triggered after the object is initialized and may be optionally overridden to do pre...
std::vector< Point > _centerpoints
MooseMesh & _mesh
A reference to the mesh.