www.mooseframework.org
EBSDMesh.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 #include "EBSDMesh.h"
8 #include "MooseApp.h"
9 
10 #include <fstream>
11 
12 template <>
13 InputParameters
15 {
16  InputParameters params = validParams<GeneratedMesh>();
17  params.addClassDescription("Mesh generated from a specified DREAM.3D EBSD data file.");
18  params.addRequiredParam<FileName>("filename", "The name of the file containing the EBSD data");
19  params.addParam<unsigned int>(
20  "uniform_refine", 0, "Number of coarsening levels available in adaptive mesh refinement.");
21 
22  // suppress parameters
23  params.suppressParameter<MooseEnum>("dim");
24  params.set<MooseEnum>("dim") = MooseEnum("1=1 2 3", "1");
25  params.suppressParameter<unsigned int>("nx");
26  params.suppressParameter<unsigned int>("ny");
27  params.suppressParameter<unsigned int>("nz");
28  params.suppressParameter<Real>("xmin");
29  params.suppressParameter<Real>("ymin");
30  params.suppressParameter<Real>("zmin");
31  params.suppressParameter<Real>("xmax");
32  params.suppressParameter<Real>("ymax");
33  params.suppressParameter<Real>("zmax");
34 
35  return params;
36 }
37 
38 EBSDMesh::EBSDMesh(const InputParameters & parameters)
39  : GeneratedMesh(parameters), _filename(getParam<FileName>("filename"))
40 {
41  if (_nx != 1 || _ny != 1 || _nz != 1)
42  mooseWarning("Do not specify mesh geometry information, it is read from the EBSD file.");
43 }
44 
46 
47 void
49 {
50  std::ifstream stream_in(_filename.c_str());
51 
52  if (!stream_in)
53  mooseError("Can't open EBSD file: ", _filename);
54 
55  // Labels to look for in the header
56  std::vector<std::string> labels = {
57  "x_step", "x_dim", "y_step", "y_dim", "z_step", "z_dim", "x_min", "y_min", "z_min"};
58 
59  // Dimension variables to store once they are found in the header
60  // X_step, X_Dim, Y_step, Y_Dim, Z_step, Z_Dim
61  // We use Reals even though the Dim values should all be integers...
62  std::vector<Real> label_vals(labels.size(), 0.0);
63 
64  std::string line;
65  while (std::getline(stream_in, line))
66  {
67  // We need to process the comment lines that have:
68  // X_step, X_Dim
69  // Y_step, Y_Dim
70  // Z_step, Z_Dim
71  // in them. The labels are case insensitive.
72  if (line.find("#") == 0)
73  {
74  // Process lines that start with a comment character (comments and meta data)
75  std::transform(line.begin(), line.end(), line.begin(), ::tolower);
76 
77  for (unsigned i = 0; i < labels.size(); ++i)
78  if (line.find(labels[i]) != std::string::npos)
79  {
80  std::string dummy;
81  std::istringstream iss(line);
82  iss >> dummy >> dummy >> label_vals[i];
83 
84  // One label per line, break out of for loop over labels
85  break;
86  }
87  }
88  else
89  // first non comment line marks the end of the header
90  break;
91  }
92 
93  // Copy stuff out of the label_vars array into class variables
94  _geometry.d[0] = label_vals[0];
95  _geometry.n[0] = label_vals[1];
96  _geometry.min[0] = label_vals[6];
97 
98  _geometry.d[1] = label_vals[2];
99  _geometry.n[1] = label_vals[3];
100  _geometry.min[1] = label_vals[7];
101 
102  _geometry.d[2] = label_vals[4];
103  _geometry.n[2] = label_vals[5];
104  _geometry.min[2] = label_vals[8];
105 
106  unsigned int dim;
107 
108  // determine mesh dimension
109  for (dim = 3; dim > 0 && _geometry.n[dim - 1] == 0; --dim)
110  ;
111 
112  // check if the data has nonzero stepsizes
113  for (unsigned i = 0; i < dim; ++i)
114  {
115  if (_geometry.n[i] == 0)
116  mooseError("Error reading header, EBSD grid size is zero.");
117  if (_geometry.d[i] == 0.0)
118  mooseError("Error reading header, EBSD data step size is zero.");
119  }
120 
121  if (dim == 0)
122  mooseError("Error reading header, EBSD data is zero dimensional.");
123 
124  _geometry.dim = dim;
125 }
126 
127 void
129 {
130  readEBSDHeader();
131 
132  unsigned int uniform_refine = getParam<unsigned int>("uniform_refine");
133  _dim = (_geometry.dim == 1 ? "1" : (_geometry.dim == 2 ? "2" : "3"));
134 
135  std::array<unsigned int, 3> nr;
136  nr[0] = _geometry.n[0];
137  nr[1] = _geometry.n[1];
138  nr[2] = _geometry.n[2];
139 
140  // set min/max box length
141  _xmin = _geometry.min[0];
142  _xmax = nr[0] * _geometry.d[0] + _geometry.min[0];
143  _ymin = _geometry.min[1];
144  _ymax = nr[1] * _geometry.d[1] + _geometry.min[1];
145  _zmin = _geometry.min[2];
146  _zmax = nr[2] * _geometry.d[2] + _geometry.min[2];
147 
148  // check if the requested uniform refine level is possible and determine initial grid size
149  for (unsigned int i = 0; i < uniform_refine; ++i)
150  for (unsigned int j = 0; j < _geometry.dim; ++j)
151  {
152  if (nr[j] % 2 != 0)
153  mooseError("EBSDMesh error. Requested uniform_refine levels not possible.");
154  nr[j] /= 2;
155  }
156 
157  _nx = nr[0];
158  _ny = nr[1];
159  _nz = nr[2];
160 
161  GeneratedMesh::buildMesh();
162 }
EBSDMeshGeometry _geometry
EBSD data file mesh information.
Definition: EBSDMesh.h:54
EBSDMesh(const InputParameters &parameters)
Definition: EBSDMesh.C:38
void readEBSDHeader()
Read the EBSD data file header.
Definition: EBSDMesh.C:48
InputParameters validParams< EBSDMesh >()
Definition: EBSDMesh.C:14
virtual ~EBSDMesh()
Definition: EBSDMesh.C:45
std::array< unsigned int, 3 > n
Definition: EBSDMesh.h:39
std::array< Real, 3 > d
Definition: EBSDMesh.h:33
virtual void buildMesh()
Definition: EBSDMesh.C:128
std::string _filename
Name of the file containing the EBSD data.
Definition: EBSDMesh.h:51
std::array< Real, 3 > min
Definition: EBSDMesh.h:35