www.mooseframework.org
Public Member Functions | Private Member Functions | Private Attributes | List of all members
GriddedData Class Reference

Container for holding a function defined on a grid of arbitrary dimension. More...

#include <GriddedData.h>

Public Member Functions

 GriddedData (std::string file_name)
 Construct with a file name. More...
 
virtual ~GriddedData ()=default
 
unsigned int getDim ()
 Returns the dimensionality of the grid. More...
 
void getAxes (std::vector< int > &axes)
 Yields axes information. More...
 
void getGrid (std::vector< std::vector< Real >> &grid)
 Yields the grid. More...
 
void getFcn (std::vector< Real > &fcn)
 Yields the values defined at the grid points. More...
 
Real evaluateFcn (const std::vector< unsigned int > &ijk)
 Evaluates the function at a given grid point. More...
 

Private Member Functions

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. More...
 
bool getSignificantLine (std::ifstream &file_stream, std::string &line)
 Extracts the next line from file_stream that is: More...
 
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, and adds these to the end of output_vec. More...
 

Private Attributes

unsigned int _dim
 
std::vector< int > _axes
 
std::vector< std::vector< Real > > _grid
 
std::vector< Real > _fcn
 
std::vector< unsigned int > _step
 

Detailed Description

Container for holding a function defined on a grid of arbitrary dimension.

Information is read from a file. The file contains the grid, which has dimension _dim, and consists of _dim vectors of Reals. The file also contains the function values at each grid point. The file also contains information on how to embed the grid into a MOOSE simulation. This is achieved through specifying the MOOSE direction that each grid axis corresponds to. For instance, the first grid axis might correspond to the MOOSE "y" direction, the second grid axis might correspond to the MOOSE "t" direction, etc.

Definition at line 39 of file GriddedData.h.

Constructor & Destructor Documentation

GriddedData::GriddedData ( std::string  file_name)

Construct with a file name.

Creates a GriddedData object by reading info from file_name A grid is defined in _grid.

For example, if grid[0] = {1, 2, 3} and grid[1] = {-1, 1} this defines a 2D grid (_dim = 2), with points (1,-1), (2,-1), (3,-1), (1,1), (2,1), (3,1) The i_th axis of the grid corresponds to the axes[i] axis of the simulation: see the function getAxes Values at each grid point are stored in _fcn. They are ordered as in the example above. _step is just a quantity used in evaluateFcn The file must have the following format: All blank lines and lines starting with # are ignored The first significant line (not blank or starting with #) should be either AXIS X or AXIS Y or AXIS Z or AXIS T The next significant line should be a space-separated array of real numbers defining the grid along that axis direction. Any number of AXIS and subsequent space-separated arrays can be defined, but if using this in conjunction with PiecewiseMultilinear, a maximum of 4 should be defined. The AXIS lines define the grid in the MOOSE simulation reference frame, and is used by PiecewiseMultilinear, for instance. The next significant line should be DATA All significant lines after DATA should be the function values at each grid point, on any number of lines of the file, but each line must be space separated. The ordering is such that when the function is evaluated, f[i,j,k,l] corresponds to the i + j*Ni + k*Ni*Nj + l*Ni*Nj*Nk data value. Here i>=0 corresponds to the index along the first AXIS, and Ni is the number of grid points along that axis, etc. See the function parse for an example.

Definition at line 63 of file GriddedData.C.

64 {
65  parse(_dim, _axes, _grid, _fcn, _step, file_name);
66 }
std::vector< std::vector< Real > > _grid
Definition: GriddedData.h:88
unsigned int _dim
Definition: GriddedData.h:86
std::vector< unsigned int > _step
Definition: GriddedData.h:90
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
virtual GriddedData::~GriddedData ( )
virtualdefault

Member Function Documentation

Real GriddedData::evaluateFcn ( const std::vector< unsigned int > &  ijk)

Evaluates the function at a given grid point.

Evaluates the function at a given grid point for instance evaluateFcn({n,m}) = value at (grid[0][n], grid[1][m]), for a function defined on a 2D grid.

For instance, evaluateFcn({n,m}) = value at (grid[0][n], grid[1][m]), for a function defined on a 2D grid

Definition at line 128 of file GriddedData.C.

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 }
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
std::vector< unsigned int > _step
Definition: GriddedData.h:90
std::vector< Real > _fcn
Definition: GriddedData.h:89
void GriddedData::getAxes ( std::vector< int > &  axes)

Yields axes information.

If axes[i] == 0 then the i_th axis in the grid data corresponds to the x axis in the simulation If axes[i] == 1 then the i_th axis in the grid data corresponds to the y axis in the simulation If axes[i] == 2 then the i_th axis in the grid data corresponds to the z axis in the simulation If axes[i] == 3 then the i_th axis in the grid data corresponds to the time in the simulation

Definition at line 88 of file GriddedData.C.

89 {
90  axes.resize(_dim);
91  for (unsigned int i = 0; i < _dim; ++i)
92  axes[i] = _axes[i];
93 }
unsigned int _dim
Definition: GriddedData.h:86
std::vector< int > _axes
Definition: GriddedData.h:87
unsigned int GriddedData::getDim ( )

Returns the dimensionality of the grid.

This may have nothing to do with the dimensionality of the simulation. Eg, a 2D grid with axes (Y,Z) (so dim=2) be used in a 3D simulation.

This may have nothing to do with the dimensionality of the simulation. Eg, a 2D grid with axes (Y,Z) (so dim=2) be used in a 3D simulation

Definition at line 75 of file GriddedData.C.

76 {
77  return _dim;
78 }
unsigned int _dim
Definition: GriddedData.h:86
void GriddedData::getFcn ( std::vector< Real > &  fcn)

Yields the values defined at the grid points.

Definition at line 115 of file GriddedData.C.

116 {
117  fcn.resize(_fcn.size());
118  for (unsigned int i = 0; i < _fcn.size(); ++i)
119  fcn[i] = _fcn[i];
120 }
std::vector< Real > _fcn
Definition: GriddedData.h:89
void GriddedData::getGrid ( std::vector< std::vector< Real >> &  grid)

Yields the grid.

grid[i] = a vector of Reals that define the i_th axis of the grid.

grid[i] = a vector of Reals that define the i_th axis of the grid

Definition at line 100 of file GriddedData.C.

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 }
std::vector< std::vector< Real > > _grid
Definition: GriddedData.h:88
unsigned int _dim
Definition: GriddedData.h:86
bool GriddedData::getSignificantLine ( std::ifstream &  file_stream,
std::string &  line 
)
private

Extracts the next line from file_stream that is:

  • not empty
  • does not start with # Returns true if such a line was found, otherwise returns false

Definition at line 278 of file GriddedData.C.

Referenced by parse().

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 }
void GriddedData::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 
)
private

parse the file_name extracting information.

Here is an example file:

this is a comment line at start of file

AXIS Y -1.5 0

there is no reason why the x axis can't be second

AXIS X 1 2 3

This example has a 3D grid, but the 3rd direction is time

AXIS T 0 199

now some data

DATA

following for x=1, t=0

1 2

following for x=2, t=0

2 3

following for x=3, t=0

2 3

following for x=1, t=199

89 900

following for x=2, t=199, and x=3, t=199

1 -3 -5 -6.898

end of file

Definition at line 172 of file GriddedData.C.

Referenced by GriddedData().

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 }
bool getSignificantLine(std::ifstream &file_stream, std::string &line)
Extracts the next line from file_stream that is:
Definition: GriddedData.C:278
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
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
void GriddedData::splitToRealVec ( const std::string &  input_string,
std::vector< Real > &  output_vec 
)
private

Splits an input_string using space as the separator Converts the resulting items to Real, and adds these to the end of output_vec.

Definition at line 299 of file GriddedData.C.

Referenced by parse().

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 }

Member Data Documentation

std::vector<int> GriddedData::_axes
private

Definition at line 87 of file GriddedData.h.

Referenced by getAxes(), and GriddedData().

unsigned int GriddedData::_dim
private

Definition at line 86 of file GriddedData.h.

Referenced by evaluateFcn(), getAxes(), getDim(), getGrid(), and GriddedData().

std::vector<Real> GriddedData::_fcn
private

Definition at line 89 of file GriddedData.h.

Referenced by evaluateFcn(), getFcn(), and GriddedData().

std::vector<std::vector<Real> > GriddedData::_grid
private

Definition at line 88 of file GriddedData.h.

Referenced by getGrid(), and GriddedData().

std::vector<unsigned int> GriddedData::_step
private

Definition at line 90 of file GriddedData.h.

Referenced by evaluateFcn(), and GriddedData().


The documentation for this class was generated from the following files: