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