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 34 of file GriddedData.h.

Constructor & Destructor Documentation

◆ GriddedData()

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 58 of file GriddedData.C.

59 {
60  parse(_dim, _axes, _grid, _fcn, _step, file_name);
61 }
std::vector< std::vector< Real > > _grid
Definition: GriddedData.h:83
unsigned int _dim
Definition: GriddedData.h:81
std::vector< unsigned int > _step
Definition: GriddedData.h:85
std::vector< int > _axes
Definition: GriddedData.h:82
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:167
std::vector< Real > _fcn
Definition: GriddedData.h:84

◆ ~GriddedData()

virtual GriddedData::~GriddedData ( )
virtualdefault

Member Function Documentation

◆ evaluateFcn()

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 123 of file GriddedData.C.

124 {
125  if (ijk.size() != _dim)
126  mooseError(
127  "Gridded data evaluateFcn called with ", ijk.size(), " arguments, but expected ", _dim);
128  unsigned int index = ijk[0];
129  for (unsigned int i = 1; i < _dim; ++i)
130  index += ijk[i] * _step[i];
131  if (index >= _fcn.size())
132  mooseError("Gridded data evaluateFcn attempted to access index ",
133  index,
134  " of function, but it contains only ",
135  _fcn.size(),
136  " entries");
137  return _fcn[index];
138 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
unsigned int _dim
Definition: GriddedData.h:81
std::vector< unsigned int > _step
Definition: GriddedData.h:85
std::vector< Real > _fcn
Definition: GriddedData.h:84

◆ getAxes()

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 83 of file GriddedData.C.

84 {
85  axes.resize(_dim);
86  for (unsigned int i = 0; i < _dim; ++i)
87  axes[i] = _axes[i];
88 }
unsigned int _dim
Definition: GriddedData.h:81
std::vector< int > _axes
Definition: GriddedData.h:82

◆ getDim()

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 70 of file GriddedData.C.

71 {
72  return _dim;
73 }
unsigned int _dim
Definition: GriddedData.h:81

◆ getFcn()

void GriddedData::getFcn ( std::vector< Real > &  fcn)

Yields the values defined at the grid points.

Definition at line 110 of file GriddedData.C.

111 {
112  fcn.resize(_fcn.size());
113  for (unsigned int i = 0; i < _fcn.size(); ++i)
114  fcn[i] = _fcn[i];
115 }
std::vector< Real > _fcn
Definition: GriddedData.h:84

◆ getGrid()

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 95 of file GriddedData.C.

96 {
97  grid.resize(_dim);
98  for (unsigned int i = 0; i < _dim; ++i)
99  {
100  grid[i].resize(_grid[i].size());
101  for (unsigned int j = 0; j < _grid[i].size(); ++j)
102  grid[i][j] = _grid[i][j];
103  }
104 }
std::vector< std::vector< Real > > _grid
Definition: GriddedData.h:83
unsigned int _dim
Definition: GriddedData.h:81

◆ getSignificantLine()

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 273 of file GriddedData.C.

Referenced by parse().

274 {
275  while (getline(file_stream, line))
276  {
277  if (line.size() == 0) // empty line: read next line from file
278  continue;
279  if (line[0] == '#') // just a comment: read next line from file
280  continue;
281  // have got a significant line
282  return true;
283  }
284  // have run out of file
285  return false;
286 }

◆ parse()

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 167 of file GriddedData.C.

Referenced by GriddedData().

173 {
174  // initialize
175  dim = 0;
176  axes.resize(0);
177  grid.resize(0);
178  f.resize(0);
179 
180  // open file and initialize quantities
181  std::ifstream file(file_name.c_str());
182  if (!file.good())
183  mooseError("Error opening file '" + file_name + "' from GriddedData.");
184  std::string line;
185  bool reading_grid_data = false;
186  bool reading_value_data = false;
187 
188  // read file line-by-line extracting data
189  while (getSignificantLine(file, line))
190  {
191  // look for AXIS keywords
192  reading_grid_data = false;
193  if (line.compare("AXIS X") == 0)
194  {
195  dim += 1;
196  reading_grid_data = true;
197  axes.push_back(0);
198  }
199  else if (line.compare("AXIS Y") == 0)
200  {
201  dim += 1;
202  reading_grid_data = true;
203  axes.push_back(1);
204  }
205  else if (line.compare("AXIS Z") == 0)
206  {
207  dim += 1;
208  reading_grid_data = true;
209  axes.push_back(2);
210  }
211  else if (line.compare("AXIS T") == 0)
212  {
213  dim += 1;
214  reading_grid_data = true;
215  axes.push_back(3);
216  }
217 
218  // just found an AXIS keyword
219  if (reading_grid_data)
220  {
221  grid.resize(dim); // add another dimension to the grid
222  grid[dim - 1].resize(0);
223  if (getSignificantLine(file, line))
224  splitToRealVec(line, grid[dim - 1]);
225  continue; // read next line from file
226  }
227 
228  // previous significant line must have been DATA
229  if (reading_value_data)
230  splitToRealVec(line, f);
231 
232  // look for DATA keyword
233  if (line.compare("DATA") == 0)
234  reading_value_data = true;
235 
236  // ignore any other lines - if we get here probably the data file is corrupt
237  }
238 
239  // check that some axes have been defined
240  if (dim == 0)
241  mooseError("No valid AXIS lines found by GriddedData");
242 
243  // step is useful in evaluateFcn
244  step.resize(dim);
245  step[0] = 1; // this is actually not used
246  for (unsigned int i = 1; i < dim; ++i)
247  step[i] = step[i - 1] * grid[i - 1].size();
248 
249  // perform some checks
250  unsigned int num_data_points = 1;
251  for (unsigned int i = 0; i < dim; ++i)
252  {
253  if (grid[i].size() == 0)
254  mooseError("Axis ", i, " in your GriddedData has zero size");
255  num_data_points *= grid[i].size();
256  }
257  if (num_data_points != f.size())
258  mooseError("According to AXIS statements in GriddedData, number of data points is ",
259  num_data_points,
260  " but ",
261  f.size(),
262  " function values were read from file");
263 }
bool getSignificantLine(std::ifstream &file_stream, std::string &line)
Extracts the next line from file_stream that is:
Definition: GriddedData.C:273
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
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:294

◆ splitToRealVec()

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 294 of file GriddedData.C.

Referenced by parse().

295 {
296  std::istringstream linestream(input_string);
297  std::string item;
298  while (getline(linestream, item, ' '))
299  {
300  std::istringstream i(item);
301  Real d;
302  i >> d;
303  output_vec.push_back(d);
304  }
305 }

Member Data Documentation

◆ _axes

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

Definition at line 82 of file GriddedData.h.

Referenced by getAxes(), and GriddedData().

◆ _dim

unsigned int GriddedData::_dim
private

Definition at line 81 of file GriddedData.h.

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

◆ _fcn

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

Definition at line 84 of file GriddedData.h.

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

◆ _grid

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

Definition at line 83 of file GriddedData.h.

Referenced by getGrid(), and GriddedData().

◆ _step

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

Definition at line 85 of file GriddedData.h.

Referenced by evaluateFcn(), and GriddedData().


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