https://mooseframework.inl.gov
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Static 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 Types

typedef MooseUtils::SemidynamicVector< Real, 4 > GridPoint
 
typedef MooseUtils::SemidynamicVector< ADReal, 4 > ADGridPoint
 
typedef MooseUtils::SemidynamicVector< unsigned int, 4 > GridIndex
 

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 GridIndex &ijk)
 Evaluates the function at a given grid point. More...
 

Static Public Member Functions

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. More...
 

Private Member Functions

void updateGrid (unsigned int &dim, const std::vector< Real > &axis_i, const int axis_index)
 

Static Private Member Functions

static bool getSignificantLine (std::ifstream &file_stream, std::string &line)
 Extracts the next line from file_stream that is: More...
 
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, 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 32 of file GriddedData.h.

Member Typedef Documentation

◆ ADGridPoint

Definition at line 43 of file GriddedData.h.

◆ GridIndex

Definition at line 44 of file GriddedData.h.

◆ GridPoint

Definition at line 42 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 55 of file GriddedData.C.

56 {
57  parse(_dim, _axes, _grid, _fcn, _step, file_name);
58 }
std::vector< std::vector< Real > > _grid
Definition: GriddedData.h:118
unsigned int _dim
Definition: GriddedData.h:116
std::vector< unsigned int > _step
Definition: GriddedData.h:120
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

◆ ~GriddedData()

virtual GriddedData::~GriddedData ( )
virtualdefault

Member Function Documentation

◆ evaluateFcn()

Real GriddedData::evaluateFcn ( const GridIndex ijk)

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

Definition at line 92 of file GriddedData.C.

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 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
unsigned int _dim
Definition: GriddedData.h:116
std::vector< unsigned int > _step
Definition: GriddedData.h:120
std::vector< Real > _fcn
Definition: GriddedData.h:119

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

68 {
69  axes.resize(_dim);
70  std::copy(_axes.begin(), _axes.end(), axes.begin());
71 }
unsigned int _dim
Definition: GriddedData.h:116
std::vector< int > _axes
Definition: GriddedData.h:117

◆ 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.

Definition at line 61 of file GriddedData.C.

62 {
63  return _dim;
64 }
unsigned int _dim
Definition: GriddedData.h:116

◆ getFcn()

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

Yields the values defined at the grid points.

Definition at line 85 of file GriddedData.C.

86 {
87  fcn.resize(_fcn.size());
88  std::copy(_fcn.begin(), _fcn.end(), fcn.begin());
89 }
std::vector< Real > _fcn
Definition: GriddedData.h:119

◆ 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.

Definition at line 74 of file GriddedData.C.

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 }
std::vector< std::vector< Real > > _grid
Definition: GriddedData.h:118
unsigned int _dim
Definition: GriddedData.h:116

◆ getSignificantLine()

bool GriddedData::getSignificantLine ( std::ifstream &  file_stream,
std::string &  line 
)
staticprivate

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

Referenced by parse().

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 }

◆ 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 
)
static

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

Referenced by GriddedData().

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 }
static bool getSignificantLine(std::ifstream &file_stream, std::string &line)
Extracts the next line from file_stream that is:
Definition: GriddedData.C:209
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:154
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

◆ splitToRealVec()

void GriddedData::splitToRealVec ( const std::string &  input_string,
std::vector< Real > &  output_vec 
)
staticprivate

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

Referenced by parse().

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 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
MPI_Status status

◆ updateGrid()

void GriddedData::updateGrid ( unsigned int dim,
const std::vector< Real > &  axis_i,
const int  axis_index 
)
private

Member Data Documentation

◆ _axes

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

Definition at line 117 of file GriddedData.h.

Referenced by getAxes(), and GriddedData().

◆ _dim

unsigned int GriddedData::_dim
private

Definition at line 116 of file GriddedData.h.

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

◆ _fcn

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

Definition at line 119 of file GriddedData.h.

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

◆ _grid

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

Definition at line 118 of file GriddedData.h.

Referenced by getGrid(), and GriddedData().

◆ _step

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

Definition at line 120 of file GriddedData.h.

Referenced by evaluateFcn(), and GriddedData().


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