Skip to content

FieldMap Class

Jacob Fields edited this page Jun 16, 2020 · 1 revision

The FieldMap class stores information about every field and its corresponding ODEData data structure for a specific Grid. In its most basic form, FieldMap is just a wrapper around std::map with some additional convenience functionality to make it easy to work with fields in OOPS-2D.

Public Methods

Method Description
FieldMap(const Grid* grid) A constructor that creates a FieldMap for a specific Grid and no fields.
FieldMap(const Grid* grid, std::map<std::string, FieldInfo>& fields) A constructor that creates a FieldMap for a specific Grid and automatically populates it with fields according to the std::map passed in through the fields argument.
~FieldMap() A destructor for FieldMap. It simply clears all the fields.
const std::map<std::string, std::shared_ptr<ODEData>>& getFields() Get all the fields stored in the FieldMap, including SolverData fields.
const std::map<std::string, std::shared_ptr<SolverData>>& getSolverFields() Get all the SolverData fields stored in FieldMap, or the fields that should be evolved by a Solver object.
void addField(std::string name, unsigned int nEqs, unsigned int stages=0) Add a new field to FieldMap. The name argument specifies the name for the new field and nEqs the number of equations. If stages == 0, then the field is initialized as an ODEData object and assumed to be an ordinary field. If stages > 0, the field is initialized as a SolverData object and assumed to be an evolved field. Therefore, stages should match the number of stages in the Solver used for this field. If a field matching name already exists, it does nothing (an exception will be thrown in a future release).
void removeField(std::string name) Remove a field indexed by name. If no field matching name exists, then an std::out_of_range exception is thrown.
bool hasField(std::string name) Check if a field matching name exists.
unsigned int getNumFields() Get the total number of fields stored in the FieldMap.
isSolverField(std::string name) Check if a field indexed by name is a a solver field (SolverData object) or an ordinary unevolved field (an ODEData object). Throws an std::out_of_range exception if name doesn't exist.
const Grid& getGrid() const Get the Grid object associated with this FieldMap.
std::shared_ptr<SolverData>& getSolverField(std::string name) Get a smart pointer to a solver field in FieldMap. An std::out_of_range exception is thrown if the field does not exist or is not a solver field.
std::shared_ptr<ODEData>& operator[](std::string name) Get a smart pointer to an ordinary field using the [] operator. Throws std::out_of_range if the field does not exist.

Tips for using FieldMap

  1. Particularly when using a large number of fields, try to avoid retrieving fields via [] and getSolverField more than necessary. The std::map class sorts data via a comparison operator, not hashing. Therefore, random access is O(\log n), not O(1). Where possible, a better practice is only calling getSolverField and [] to create a new reference and then manipulating the field through that reference.
  2. The ODE class maintains a set of FieldInfo objects and acts as a factory for FieldMap. In order to keep all data consistent inside the ODE class, it's good practice to add and remove fields through ODE, not FieldMap.

Possible Improvements

  • The addField method should throw an exception when a field already exists.
  • For consistency, there should be a getField method to parallel getSolverField. Because ODE typically frequently only has access to a FieldMap as a pointer, this also allows for a more natural syntax using the -> operator than is possible with the [] operator.

Clone this wiki locally