www.mooseframework.org
GriddedData.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "GriddedData.h"
16 
17 // MOOSE includes
18 #include "MooseError.h"
19 
20 // C++ includes
21 #include <fstream>
22 
63 GriddedData::GriddedData(std::string file_name)
64 {
65  parse(_dim, _axes, _grid, _fcn, _step, file_name);
66 }
67 
74 unsigned int
76 {
77  return _dim;
78 }
79 
87 void
88 GriddedData::getAxes(std::vector<int> & axes)
89 {
90  axes.resize(_dim);
91  for (unsigned int i = 0; i < _dim; ++i)
92  axes[i] = _axes[i];
93 }
94 
99 void
100 GriddedData::getGrid(std::vector<std::vector<Real>> & grid)
101 {
102  grid.resize(_dim);
103  for (unsigned int i = 0; i < _dim; ++i)
104  {
105  grid[i].resize(_grid[i].size());
106  for (unsigned int j = 0; j < _grid[i].size(); ++j)
107  grid[i][j] = _grid[i][j];
108  }
109 }
110 
114 void
115 GriddedData::getFcn(std::vector<Real> & fcn)
116 {
117  fcn.resize(_fcn.size());
118  for (unsigned int i = 0; i < _fcn.size(); ++i)
119  fcn[i] = _fcn[i];
120 }
121 
127 Real
128 GriddedData::evaluateFcn(const std::vector<unsigned int> & ijk)
129 {
130  if (ijk.size() != _dim)
131  mooseError(
132  "Gridded data evaluateFcn called with ", ijk.size(), " arguments, but expected ", _dim);
133  unsigned int index = ijk[0];
134  for (unsigned int i = 1; i < _dim; ++i)
135  index += ijk[i] * _step[i];
136  if (index >= _fcn.size())
137  mooseError("Gridded data evaluateFcn attempted to access index ",
138  index,
139  " of function, but it contains only ",
140  _fcn.size(),
141  " entries");
142  return _fcn[index];
143 }
144 
171 void
172 GriddedData::parse(unsigned int & dim,
173  std::vector<int> & axes,
174  std::vector<std::vector<Real>> & grid,
175  std::vector<Real> & f,
176  std::vector<unsigned int> & step,
177  std::string file_name)
178 {
179  // initialize
180  dim = 0;
181  axes.resize(0);
182  grid.resize(0);
183  f.resize(0);
184 
185  // open file and initialize quantities
186  std::ifstream file(file_name.c_str());
187  if (!file.good())
188  mooseError("Error opening file '" + file_name + "' from GriddedData.");
189  std::string line;
190  bool reading_grid_data = false;
191  bool reading_value_data = false;
192 
193  // read file line-by-line extracting data
194  while (getSignificantLine(file, line))
195  {
196  // look for AXIS keywords
197  reading_grid_data = false;
198  if (line.compare("AXIS X") == 0)
199  {
200  dim += 1;
201  reading_grid_data = true;
202  axes.push_back(0);
203  }
204  else if (line.compare("AXIS Y") == 0)
205  {
206  dim += 1;
207  reading_grid_data = true;
208  axes.push_back(1);
209  }
210  else if (line.compare("AXIS Z") == 0)
211  {
212  dim += 1;
213  reading_grid_data = true;
214  axes.push_back(2);
215  }
216  else if (line.compare("AXIS T") == 0)
217  {
218  dim += 1;
219  reading_grid_data = true;
220  axes.push_back(3);
221  }
222 
223  // just found an AXIS keyword
224  if (reading_grid_data)
225  {
226  grid.resize(dim); // add another dimension to the grid
227  grid[dim - 1].resize(0);
228  if (getSignificantLine(file, line))
229  splitToRealVec(line, grid[dim - 1]);
230  continue; // read next line from file
231  }
232 
233  // previous significant line must have been DATA
234  if (reading_value_data)
235  splitToRealVec(line, f);
236 
237  // look for DATA keyword
238  if (line.compare("DATA") == 0)
239  reading_value_data = true;
240 
241  // ignore any other lines - if we get here probably the data file is corrupt
242  }
243 
244  // check that some axes have been defined
245  if (dim == 0)
246  mooseError("No valid AXIS lines found by GriddedData");
247 
248  // step is useful in evaluateFcn
249  step.resize(dim);
250  step[0] = 1; // this is actually not used
251  for (unsigned int i = 1; i < dim; ++i)
252  step[i] = step[i - 1] * grid[i - 1].size();
253 
254  // perform some checks
255  unsigned int num_data_points = 1;
256  for (unsigned int i = 0; i < dim; ++i)
257  {
258  if (grid[i].size() == 0)
259  mooseError("Axis ", i, " in your GriddedData has zero size");
260  num_data_points *= grid[i].size();
261  }
262  if (num_data_points != f.size())
263  mooseError("According to AXIS statements in GriddedData, number of data points is ",
264  num_data_points,
265  " but ",
266  f.size(),
267  " function values were read from file");
268 }
269 
277 bool
278 GriddedData::getSignificantLine(std::ifstream & file_stream, std::string & line)
279 {
280  while (getline(file_stream, line))
281  {
282  if (line.size() == 0) // empty line: read next line from file
283  continue;
284  if (line[0] == '#') // just a comment: read next line from file
285  continue;
286  // have got a significant line
287  return true;
288  }
289  // have run out of file
290  return false;
291 }
292 
298 void
299 GriddedData::splitToRealVec(const std::string & input_string, std::vector<Real> & output_vec)
300 {
301  std::istringstream linestream(input_string);
302  std::string item;
303  while (getline(linestream, item, ' '))
304  {
305  std::istringstream i(item);
306  Real d;
307  i >> d;
308  output_vec.push_back(d);
309  }
310 }
bool getSignificantLine(std::ifstream &file_stream, std::string &line)
Extracts the next line from file_stream that is:
Definition: GriddedData.C:278
std::vector< std::vector< Real > > _grid
Definition: GriddedData.h:88
Real evaluateFcn(const std::vector< unsigned int > &ijk)
Evaluates the function at a given grid point.
Definition: GriddedData.C:128
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
unsigned int _dim
Definition: GriddedData.h:86
void getAxes(std::vector< int > &axes)
Yields axes information.
Definition: GriddedData.C:88
std::vector< unsigned int > _step
Definition: GriddedData.h:90
void splitToRealVec(const std::string &input_string, std::vector< Real > &output_vec)
Splits an input_string using space as the separator Converts the resulting items to Real...
Definition: GriddedData.C:299
GriddedData(std::string file_name)
Construct with a file name.
Definition: GriddedData.C:63
unsigned int getDim()
Returns the dimensionality of the grid.
Definition: GriddedData.C:75
std::vector< int > _axes
Definition: GriddedData.h:87
void parse(unsigned int &dim, std::vector< int > &axes, std::vector< std::vector< Real >> &grid, std::vector< Real > &f, std::vector< unsigned int > &step, std::string file_name)
parse the file_name extracting information.
Definition: GriddedData.C:172
std::vector< Real > _fcn
Definition: GriddedData.h:89
void getFcn(std::vector< Real > &fcn)
Yields the values defined at the grid points.
Definition: GriddedData.C:115
void getGrid(std::vector< std::vector< Real >> &grid)
Yields the grid.
Definition: GriddedData.C:100