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:

X
Y
Z

Public Functions

GridDataSource(const bool source_is_file = false)
virtual ~GridDataSource()
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) = 0
virtual bool get(Mesh *m, Field3D &var, const std::string &name, BoutReal def = 0.0) = 0
virtual bool get(Mesh *m, FieldPerp &var, const std::string &name, BoutReal def = 0.0) = 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

constexpr Direction X = Direction::X
constexpr Direction Y = Direction::Y
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()
GridFile(std::unique_ptr<DataFormat> format, std::string gridfilename)

Creates a GridFile object

format Pointer to DataFormat. This will be deleted in destructor

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

Tests whether a variable exists in the file

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

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

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.

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

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.

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

Get a BoutReal number.

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

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.

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

Are x-boundary guard cells read from the source?

bool hasYBoundaryGuards()

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 = 0.0)
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

GridFromOptions(Options *opt = nullptr)

Constructor, passing optional Options object

Parameters
  • opt: Options section to use as input. By default the “mesh” section under root will be used.

bool hasVar(const std::string &name)

Checks if the options has a given variable

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

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

Return
True if option is set, false if ival is default (0)
Parameters
  • mesh: Not used
  • name: Name of variable
  • sval: Always given a value, defaults to 0

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

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

Return
True if option is set, false if ival is default (0)
Parameters
  • mesh: Not used
  • ival: The variable which will be set
  • name: Name of variable
  • def: Default value to use if option not found

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

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

Return
True if option is set, false if ival is default (0)
Parameters
  • mesh: Not used
  • name: Name of variable
  • rval: Always given a value, defaults to 0

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

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

Parameters
  • mesh: The Mesh object over which the field is defined
  • var: The variable which will be set
  • name: The name in the options. Not case sensitive
  • def: Default value to use if option not found

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

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

Parameters
  • mesh: The Mesh object over which the field is defined
  • var: The variable which will be set
  • name: The name in the options. Not case sensitive
  • def: Default value to use if option not found

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

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

Parameters
  • mesh: The Mesh object over which the field is defined
  • var: The variable which will be set
  • name: The name in the options. Not case sensitive
  • def: Default value to use if option not found

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

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

Parameters
  • mesh: Mesh object
  • var: A vector which will be resized to length len
  • name: The name of the option
  • len: The length of the vector
  • offset: Not currently used
  • dir: The direction (X,Y,Z) of the array

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

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: Mesh object
  • var: A vector which will be resized to length len
  • name: The name of the option
  • len: The length of the vector
  • offset: The index where this vector starts i.e. var[0] is at x=offset if dir is X.
  • dir: The direction (X,Y,Z) of the array

bool hasXBoundaryGuards(Mesh *m)

Are x-boundary guard cells read from the source?

bool hasYBoundaryGuards()

Are y-boundary guard cells read from the source?

Private Members

Options *options

The options section to use. Could be nullptr.