https://mooseframework.inl.gov
GriddedData.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 
10 #include "GriddedData.h"
11 #include "MooseUtils.h"
12 #include <fstream>
13 #include <algorithm>
14 
55 GriddedData::GriddedData(std::string file_name)
56 {
57  parse(_dim, _axes, _grid, _fcn, _step, file_name);
58 }
59 
60 unsigned int
62 {
63  return _dim;
64 }
65 
66 void
67 GriddedData::getAxes(std::vector<int> & axes)
68 {
69  axes.resize(_dim);
70  std::copy(_axes.begin(), _axes.end(), axes.begin());
71 }
72 
73 void
74 GriddedData::getGrid(std::vector<std::vector<Real>> & grid)
75 {
76  grid.resize(_dim);
77  for (unsigned int i = 0; i < _dim; ++i)
78  {
79  grid[i].resize(_grid[i].size());
80  std::copy(_grid[i].begin(), _grid[i].end(), grid[i].begin());
81  }
82 }
83 
84 void
85 GriddedData::getFcn(std::vector<Real> & fcn)
86 {
87  fcn.resize(_fcn.size());
88  std::copy(_fcn.begin(), _fcn.end(), fcn.begin());
89 }
90 
91 Real
93 {
94  if (ijk.size() != _dim)
95  mooseError(
96  "Gridded data evaluateFcn called with ", ijk.size(), " arguments, but expected ", _dim);
97  unsigned int index = ijk[0];
98  for (unsigned int i = 1; i < _dim; ++i)
99  index += ijk[i] * _step[i];
100  if (index >= _fcn.size())
101  mooseError("Gridded data evaluateFcn attempted to access index ",
102  index,
103  " of function, but it contains only ",
104  _fcn.size(),
105  " entries");
106  return _fcn[index];
107 }
108 
109 void
110 GriddedData::parse(unsigned int & dim,
111  std::vector<int> & axes,
112  std::vector<std::vector<Real>> & grid,
113  std::vector<Real> & f,
114  std::vector<unsigned int> & step,
115  std::string file_name)
116 {
117  // initialize
118  dim = 0;
119  axes.resize(0);
120  grid.resize(0);
121  f.resize(0);
122 
123  // open file and initialize quantities
124  std::ifstream file(file_name.c_str());
125  if (!file.good())
126  mooseError("Error opening file '" + file_name + "' from GriddedData.");
127  std::string line;
128  bool reading_grid_data = false;
129  bool reading_value_data = false;
130 
131  // read file line-by-line extracting data
132  while (getSignificantLine(file, line))
133  {
134  // look for AXIS keywords
135  reading_grid_data = false;
136  if (line.compare("AXIS X") == 0)
137  {
138  dim += 1;
139  reading_grid_data = true;
140  axes.push_back(0);
141  }
142  else if (line.compare("AXIS Y") == 0)
143  {
144  dim += 1;
145  reading_grid_data = true;
146  axes.push_back(1);
147  }
148  else if (line.compare("AXIS Z") == 0)
149  {
150  dim += 1;
151  reading_grid_data = true;
152  axes.push_back(2);
153  }
154  else if (line.compare("AXIS T") == 0)
155  {
156  dim += 1;
157  reading_grid_data = true;
158  axes.push_back(3);
159  }
160 
161  // just found an AXIS keyword
162  if (reading_grid_data)
163  {
164  grid.resize(dim); // add another dimension to the grid
165  grid[dim - 1].resize(0);
166  if (getSignificantLine(file, line))
167  splitToRealVec(line, grid[dim - 1]);
168  continue; // read next line from file
169  }
170 
171  // previous significant line must have been DATA
172  if (reading_value_data)
173  splitToRealVec(line, f);
174 
175  // look for DATA keyword
176  if (line.compare("DATA") == 0)
177  reading_value_data = true;
178 
179  // ignore any other lines - if we get here probably the data file is corrupt
180  }
181 
182  // check that some axes have been defined
183  if (dim == 0)
184  mooseError("No valid AXIS lines found by GriddedData");
185 
186  // step is useful in evaluateFcn
187  step.resize(dim);
188  step[0] = 1; // this is actually not used
189  for (unsigned int i = 1; i < dim; ++i)
190  step[i] = step[i - 1] * grid[i - 1].size();
191 
192  // perform some checks
193  unsigned int num_data_points = 1;
194  for (unsigned int i = 0; i < dim; ++i)
195  {
196  if (grid[i].size() == 0)
197  mooseError("Axis ", i, " in your GriddedData has zero size");
198  num_data_points *= grid[i].size();
199  }
200  if (num_data_points != f.size())
201  mooseError("According to AXIS statements in GriddedData, number of data points is ",
202  num_data_points,
203  " but ",
204  f.size(),
205  " function values were read from file");
206 }
207 
208 bool
209 GriddedData::getSignificantLine(std::ifstream & file_stream, std::string & line)
210 {
211  while (std::getline(file_stream, line))
212  {
213  if (line.size() == 0) // empty line: read next line from file
214  continue;
215  if (line[0] == '#') // just a comment: read next line from file
216  continue;
217  // have got a significant line
218  return true;
219  }
220  // have run out of file
221  return false;
222 }
223 
224 void
225 GriddedData::splitToRealVec(const std::string & input_string, std::vector<Real> & output_vec)
226 {
227  std::vector<Real> values;
228  bool status = MooseUtils::tokenizeAndConvert<Real>(input_string, values, " ");
229 
230  if (!status)
231  mooseError("GriddedData: Failed to convert string into Real when reading line ", input_string);
232 
233  for (auto val : values)
234  output_vec.push_back(val);
235 }
static bool getSignificantLine(std::ifstream &file_stream, std::string &line)
Extracts the next line from file_stream that is:
Definition: GriddedData.C:209
std::vector< std::vector< Real > > _grid
Definition: GriddedData.h:118
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:153
unsigned int _dim
Definition: GriddedData.h:116
Utility class template for a semidynamic vector with a maximum size N and a chosen dynamic size...
Definition: MooseUtils.h:1096
Real evaluateFcn(const GridIndex &ijk)
Evaluates the function at a given grid point.
Definition: GriddedData.C:92
MPI_Status status
void getAxes(std::vector< int > &axes)
Yields axes information.
Definition: GriddedData.C:67
std::vector< unsigned int > _step
Definition: GriddedData.h:120
static 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:225
GriddedData(std::string file_name)
Construct with a file name.
Definition: GriddedData.C:55
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
unsigned int getDim()
Returns the dimensionality of the grid.
Definition: GriddedData.C:61
std::vector< int > _axes
Definition: GriddedData.h:117
static 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:110
std::vector< Real > _fcn
Definition: GriddedData.h:119
void getFcn(std::vector< Real > &fcn)
Yields the values defined at the grid points.
Definition: GriddedData.C:85
void getGrid(std::vector< std::vector< Real >> &grid)
Yields the grid.
Definition: GriddedData.C:74