www.mooseframework.org
ElementPropertyReadFile.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 
9 #include "MooseRandom.h"
10 #include "MooseMesh.h"
11 
12 #include <fstream>
13 
14 template <>
15 InputParameters
17 {
18  InputParameters params = validParams<GeneralUserObject>();
19  params.addClassDescription("User Object to read property data from an external file and assign "
20  "to elements: Works only for Rectangular geometry (2D-3D)");
21  params.addParam<FileName>("prop_file_name", "", "Name of the property file name");
22  params.addRequiredParam<unsigned int>("nprop", "Number of tabulated property values");
23  params.addParam<unsigned int>("ngrain", 0, "Number of grains");
24  params.addParam<MooseEnum>("read_type",
25  MooseEnum("element grain none", "none"),
26  "Type of property distribution: element:element by element property "
27  "variation; grain:voronoi grain structure");
28  params.addParam<unsigned int>("rand_seed", 2000, "random seed");
29  params.addParam<MooseEnum>(
30  "rve_type",
31  MooseEnum("periodic none", "none"),
32  "Periodic or non-periodic grain distribution: Default is non-periodic");
33  return params;
34 }
35 
36 ElementPropertyReadFile::ElementPropertyReadFile(const InputParameters & parameters)
37  : GeneralUserObject(parameters),
38  _prop_file_name(getParam<FileName>("prop_file_name")),
39  _nprop(getParam<unsigned int>("nprop")),
40  _ngrain(getParam<unsigned int>("ngrain")),
41  _read_type(getParam<MooseEnum>("read_type")),
42  _rand_seed(getParam<unsigned int>("rand_seed")),
43  _rve_type(getParam<MooseEnum>("rve_type")),
44  _mesh(_fe_problem.mesh())
45 {
46  _nelem = _mesh.nElem();
47 
48  for (unsigned int i = 0; i < LIBMESH_DIM; i++)
49  {
50  _bottom_left(i) = _mesh.getMinInDimension(i);
51  _top_right(i) = _mesh.getMaxInDimension(i);
52  _range(i) = _top_right(i) - _bottom_left(i);
53  }
54 
55  _max_range = _range(0);
56  for (unsigned int i = 1; i < LIBMESH_DIM; i++)
57  if (_range(i) > _max_range)
58  _max_range = _range(i);
59 
60  switch (_read_type)
61  {
62  case 0:
64  break;
65 
66  case 1:
67  readGrainData();
68  break;
69 
70  default:
71  mooseError("Error ElementPropertyReadFile: Provide valid read type");
72  }
73 }
74 
75 void
77 {
78  _data.resize(_nprop * _nelem);
79 
80  MooseUtils::checkFileReadable(_prop_file_name);
81 
82  std::ifstream file_prop;
83  file_prop.open(_prop_file_name.c_str());
84 
85  for (unsigned int i = 0; i < _nelem; i++)
86  for (unsigned int j = 0; j < _nprop; j++)
87  if (!(file_prop >> _data[i * _nprop + j]))
88  mooseError("Error ElementPropertyReadFile: Premature end of file");
89 
90  file_prop.close();
91 }
92 
93 void
95 {
96  mooseAssert(_ngrain > 0, "Error ElementPropertyReadFile: Provide non-zero number of grains");
97  _data.resize(_nprop * _ngrain);
98 
99  MooseUtils::checkFileReadable(_prop_file_name);
100  std::ifstream file_prop;
101  file_prop.open(_prop_file_name.c_str());
102 
103  for (unsigned int i = 0; i < _ngrain; i++)
104  for (unsigned int j = 0; j < _nprop; j++)
105  if (!(file_prop >> _data[i * _nprop + j]))
106  mooseError("Error ElementPropertyReadFile: Premature end of file");
107 
108  file_prop.close();
110 }
111 
112 void
114 {
115  _center.resize(_ngrain);
116  MooseRandom::seed(_rand_seed);
117  for (unsigned int i = 0; i < _ngrain; i++)
118  for (unsigned int j = 0; j < LIBMESH_DIM; j++)
119  _center[i](j) = _bottom_left(j) + MooseRandom::rand() * _range(j);
120 }
121 
122 Real
123 ElementPropertyReadFile::getData(const Elem * elem, unsigned int prop_num) const
124 {
125  switch (_read_type)
126  {
127  case 0:
128  return getElementData(elem, prop_num);
129 
130  case 1:
131  return getGrainData(elem, prop_num);
132  }
133  mooseError("Error ElementPropertyReadFile: Provide valid read type");
134 }
135 
136 Real
137 ElementPropertyReadFile::getElementData(const Elem * elem, unsigned int prop_num) const
138 {
139  unsigned int jelem = elem->id();
140  mooseAssert(jelem < _nelem,
141  "Error ElementPropertyReadFile: Element "
142  << jelem
143  << " greater than than total number of element in mesh "
144  << _nelem);
145  mooseAssert(prop_num < _nprop,
146  "Error ElementPropertyReadFile: Property number "
147  << prop_num
148  << " greater than than total number of properties "
149  << _nprop);
150  return _data[jelem * _nprop + prop_num];
151 }
152 
153 Real
154 ElementPropertyReadFile::getGrainData(const Elem * elem, unsigned int prop_num) const
155 {
156  mooseAssert(prop_num < _nprop,
157  "Error ElementPropertyReadFile: Property number "
158  << prop_num
159  << " greater than than total number of properties "
160  << _nprop
161  << "\n");
162 
163  Point centroid = elem->centroid();
164  Real min_dist = _max_range;
165  unsigned int igrain = 0;
166 
167  for (unsigned int i = 0; i < _ngrain; ++i)
168  {
169  Real dist = 0.0;
170  switch (_rve_type)
171  {
172  case 0:
173  // Calculates minimum periodic distance when "periodic" is specified
174  // for rve_type
175  dist = minPeriodicDistance(_center[i], centroid);
176  break;
177 
178  default:
179  // Calculates minimum distance when nothing is specified
180  // for rve_type
181  Point dist_vec = _center[i] - centroid;
182  dist = dist_vec.norm();
183  }
184 
185  if (dist < min_dist)
186  {
187  min_dist = dist;
188  igrain = i;
189  }
190  }
191 
192  return _data[igrain * _nprop + prop_num];
193 }
194 
195 // TODO: this should probably use the built-in min periodic distance!
196 Real
198 {
199  Point dist_vec = c - p;
200  Real min_dist = dist_vec.norm();
201 
202  Real fac[3] = {-1.0, 0.0, 1.0};
203  for (unsigned int i = 0; i < 3; i++)
204  for (unsigned int j = 0; j < 3; j++)
205  for (unsigned int k = 0; k < 3; k++)
206  {
207  Point p1;
208  p1(0) = p(0) + fac[i] * _range(0);
209  p1(1) = p(1) + fac[j] * _range(1);
210  p1(2) = p(2) + fac[k] * _range(2);
211 
212  dist_vec = c - p1;
213  Real dist = dist_vec.norm();
214 
215  if (dist < min_dist)
216  min_dist = dist;
217  }
218 
219  return min_dist;
220 }
Real getElementData(const Elem *, unsigned int) const
This function assign properties to element read from file with element based properties.
unsigned int _nprop
Number of properties in a row.
ElementPropertyReadFile(const InputParameters &parameters)
Real getData(const Elem *, unsigned int) const
This function assign property data to elements.
Real minPeriodicDistance(Point, Point) const
This function calculates minimum distance between 2 points considering periodicity of the simulation ...
std::vector< Point > _center
unsigned int _ngrain
Number of grains (for property read based on grains)
MooseEnum _read_type
Type of read - element or grain.
InputParameters validParams< ElementPropertyReadFile >()
std::vector< Real > _data
Store property values read from file.
void readElementData()
This function reads element data from file.
virtual void readGrainData()
This function Read grain data from file.
unsigned int _rand_seed
Random seed - used for generating grain centers.
MooseEnum _rve_type
Type of grain structure - non-periodic default.
Real getGrainData(const Elem *, unsigned int) const
This function assign properties to element read from file with grain based properties Grain distribut...
virtual void initGrainCenterPoints()
This function generates grain center point Presently random generated.
std::string _prop_file_name
Name of file containing property values.