File griddata.hxx

class GridDataSource
#include <griddata.hxx>

Interface class to serve grid data.

Provides a generic interface for sources of equilibrium data. Could be used to simplify interfacing between BOUT++ and other codes

Subclassed by GridFile, GridFromOptions

Public Types

enum Direction

Values:

enumerator X
enumerator Y
enumerator Z

Public Functions

inline GridDataSource(const bool source_is_file = false)
virtual ~GridDataSource() = default
virtual bool hasVar(const std::string &name) = 0

Test if source can supply a variable.

virtual bool get(Mesh *m, std::string &sval, const std::string &name, const std::string &def = "") = 0

Get a string.

virtual bool get(Mesh *m, int &ival, const std::string &name, int def = 0) = 0

Get an integer.

virtual bool get(Mesh *m, BoutReal &rval, const std::string &name, BoutReal def = 0.0) = 0

Get a BoutReal number.

virtual bool get(Mesh *m, Field2D &var, const std::string &name, BoutReal def = 0.0, CELL_LOC location = CELL_DEFAULT) = 0
virtual bool get(Mesh *m, Field3D &var, const std::string &name, BoutReal def = 0.0, CELL_LOC location = CELL_DEFAULT) = 0
virtual bool get(Mesh *m, FieldPerp &var, const std::string &name, BoutReal def = 0.0, CELL_LOC location = CELL_DEFAULT) = 0
virtual bool get(Mesh *m, std::vector<int> &var, const std::string &name, int len, int offset = 0, Direction dir = GridDataSource::X) = 0
virtual bool get(Mesh *m, std::vector<BoutReal> &var, const std::string &name, int len, int offset = 0, Direction dir = GridDataSource::X) = 0
virtual bool hasXBoundaryGuards(Mesh *m) = 0

Are x-boundary guard cells read from the source?

virtual bool hasYBoundaryGuards() = 0

Are y-boundary guard cells read from the source?

Public Members

const bool is_file

Is the data source a grid file?

Public Static Attributes

static constexpr Direction X = Direction::X
static constexpr Direction Y = Direction::Y
static constexpr Direction Z = Direction::Z
class GridFile : public GridDataSource
#include <griddata.hxx>

Interface to grid data in a file.

This is a thin wrapper around a DataFormat object. Only needs to implement reading routines.

Public Functions

GridFile() = delete
GridFile(std::unique_ptr<DataFormat> format, std::string gridfilename)

Creates a GridFile object

format Pointer to DataFormat. This will be deleted in destructor

~GridFile() override
virtual bool hasVar(const std::string &name) override

Tests whether a variable exists in the file

Currently this is done by getting the variable’s size, and testing for zero size.

virtual bool get(Mesh *m, std::string &sval, const std::string &name, const std::string &def = "") override

Get a string.

Read a string from file. If the string is not found, then string is set to “” and false is returned.

Inputs

m Pointer to mesh, not used name String containing name of variable

Outputs

sval Reference to string

Returns

Boolean. True on success.

virtual bool get(Mesh *m, int &ival, const std::string &name, int def = 0) override

Get an integer.

Read a single integer from file. If the integer is not found, then ival is set to zero and false is returned.

Inputs

m Pointer to mesh, not used name String containing name of variable

Outputs

ival Reference to integer

Returns

Boolean. True on success.

virtual bool get(Mesh *m, BoutReal &rval, const std::string &name, BoutReal def = 0.0) override

Get a BoutReal number.

virtual bool get(Mesh *m, Field2D &var, const std::string &name, BoutReal def = 0.0, CELL_LOC location = CELL_DEFAULT) override

Reads a 2D, 3D or FieldPerp field variable from a file

Successfully reads Field2D or FieldPerp if the variable in the file is 0-D or 2-D. Successfully reads Field3D if the variable in the file is 0-D, 2-D or 3-D.

virtual bool get(Mesh *m, Field3D &var, const std::string &name, BoutReal def = 0.0, CELL_LOC location = CELL_DEFAULT) override
inline virtual bool get(Mesh *m, FieldPerp &var, const std::string &name, BoutReal def = 0.0, CELL_LOC location = CELL_DEFAULT) override
virtual bool get(Mesh *m, std::vector<int> &var, const std::string &name, int len, int offset = 0, GridDataSource::Direction dir = GridDataSource::X) override
virtual bool get(Mesh *m, std::vector<BoutReal> &var, const std::string &name, int len, int offset = 0, GridDataSource::Direction dir = GridDataSource::X) override
virtual bool hasXBoundaryGuards(Mesh *m) override

Are x-boundary guard cells read from the source?

inline virtual bool hasYBoundaryGuards() override

Are y-boundary guard cells read from the source?

Private Functions

bool readgrid_3dvar_fft(Mesh *m, const std::string &name, int yread, int ydest, int ysize, int xread, int xdest, int xsize, Field3D &var)

Reads in a portion of the X-Y domain.

bool readgrid_3dvar_real(const std::string &name, int yread, int ydest, int ysize, int xread, int xdest, int xsize, Field3D &var)

Reads a 3D variable directly from the file, without any processing

bool readgrid_perpvar_fft(Mesh *m, const std::string &name, int xread, int xdest, int xsize, FieldPerp &var)
bool readgrid_perpvar_real(const std::string &name, int xread, int xdest, int xsize, FieldPerp &var)

Reads a FieldPerp variable directly from the file, without any processing

template<typename T>
bool getField(Mesh *m, T &var, const std::string &name, BoutReal def, CELL_LOC location)
void readField(Mesh *m, const std::string &name, int ys, int yd, int ny_to_read, int xs, int xd, int nx_to_read, const std::vector<int> &size, Field2D &var)
void readField(Mesh *m, const std::string &name, int ys, int yd, int ny_to_read, int xs, int xd, int nx_to_read, const std::vector<int> &size, Field3D &var)
void readField(Mesh *m, const std::string &name, int ys, int yd, int ny_to_read, int xs, int xd, int nx_to_read, const std::vector<int> &size, FieldPerp &var)

Private Members

std::unique_ptr<DataFormat> file
std::string filename
int grid_yguards = {0}
int ny_inner = {0}
class GridFromOptions : public GridDataSource
#include <griddata.hxx>

Provides a way to create variables from options, which can be set in the input file or on the command line. This is done using FieldFactory to convert string expressions into fields.

Public Functions

inline GridFromOptions(Options *opt = nullptr)

Constructor, passing optional Options object

Parameters

opt[in] Options section to use as input. By default the “mesh” section under root will be used.

virtual bool hasVar(const std::string &name) override

Checks if the options has a given variable

virtual bool get(Mesh *mesh, std::string &sval, const std::string &name, const std::string &def = "") override

Reads strings from options. Uses Options::get to handle expressions

Parameters
  • mesh[in] Not used

  • name[in] Name of variable

  • sval[out] Always given a value, defaults to 0

Returns

True if option is set, false if ival is default (0)

virtual bool get(Mesh *mesh, int &ival, const std::string &name, int def = 0) override

Reads integers from options. Uses Options::get to handle expressions

Parameters
  • mesh[in] Not used

  • ival[out] The variable which will be set

  • name[in] Name of variable

  • def[in] Default value to use if option not found

Returns

True if option is set, false if ival is default (0)

virtual bool get(Mesh *mesh, BoutReal &rval, const std::string &name, BoutReal def = 0.0) override

Reads BoutReal from options. Uses Options::get to handle expressions

Parameters
  • mesh[in] Not used

  • name[in] Name of variable

  • rval[out] Always given a value, defaults to 0

Returns

True if option is set, false if ival is default (0)

virtual bool get(Mesh *mesh, Field2D &var, const std::string &name, BoutReal def = 0.0, CELL_LOC location = CELL_DEFAULT) override

Get a Field2D object by finding the option with the given name, and passing the string to FieldFactory

Parameters
  • mesh[in] The Mesh object over which the field is defined

  • var[out] The variable which will be set

  • name[in] The name in the options. Not case sensitive

  • def[in] Default value to use if option not found

  • location[in] Location at which to get the variable

virtual bool get(Mesh *mesh, Field3D &var, const std::string &name, BoutReal def = 0.0, CELL_LOC location = CELL_DEFAULT) override

Get a Field3D object by finding the option with the given name, and passing the string to FieldFactory

Parameters
  • mesh[in] The Mesh object over which the field is defined

  • var[out] The variable which will be set

  • name[in] The name in the options. Not case sensitive

  • def[in] Default value to use if option not found

  • location[in] Location at which to get the variable

virtual bool get(Mesh *mesh, FieldPerp &var, const std::string &name, BoutReal def = 0.0, CELL_LOC location = CELL_DEFAULT) override

Get a FieldPerp object by finding the option with the given name, and passing the string to FieldFactory

Parameters
  • mesh[in] The Mesh object over which the field is defined

  • var[out] The variable which will be set

  • name[in] The name in the options. Not case sensitive

  • def[in] Default value to use if option not found

  • location[in] Location at which to get the variable

virtual bool get(Mesh *mesh, std::vector<int> &var, const std::string &name, int len, int offset = 0, GridDataSource::Direction dir = GridDataSource::X) override

Get an array of integers. Currently reads a single integer, and sets the whole array to the same value

Parameters
  • mesh[in] Mesh object

  • var[out] A vector which will be resized to length len

  • name[in] The name of the option

  • len[in] The length of the vector

  • offset[in] Not currently used

  • dir[in] The direction (X,Y,Z) of the array

virtual bool get(Mesh *mesh, std::vector<BoutReal> &var, const std::string &name, int len, int offset = 0, GridDataSource::Direction dir = GridDataSource::X) override

Get an array of BoutReals. Uses FieldFactory to generate an expression, then evaluates it at indices depending on the direction (dir) and length (len)

Parameters
  • mesh[in] Mesh object

  • var[out] A vector which will be resized to length len

  • name[in] The name of the option

  • len[in] The length of the vector

  • offset[in] The index where this vector starts i.e. var[0] is at x=offset if dir is X.

  • dir[in] The direction (X,Y,Z) of the array

inline virtual bool hasXBoundaryGuards(Mesh *m) override

Are x-boundary guard cells read from the source?

inline virtual bool hasYBoundaryGuards() override

Are y-boundary guard cells read from the source?

Private Members

Options *options

The options section to use. Could be nullptr.