simple_state.h
Go to the documentation of this file.
1 /*
2 This file is part of the Ristra Wonton project.
3 Please see the license file at the root of this repository, or at:
4  https://github.com/laristra/wonton/blob/master/LICENSE
5 */
6 
7 #ifndef WONTON_SIMPLE_STATE_H_
8 #define WONTON_SIMPLE_STATE_H_
9 
10 #include <vector>
11 #include <memory>
12 #include <string>
13 #include <utility>
14 #include <map>
15 #include <stdexcept>
16 
18 
19 #include "wonton/support/wonton.h"
20 
27 namespace Wonton {
28 
43 class Simple_State {
44  public:
49  explicit Simple_State(std::shared_ptr<Simple_Mesh> mesh) : mesh_(mesh) { }
50 
53 
55  int numVars() const { return names_.size(); }
56 
58  typedef std::vector<double> vec;
59  typedef vec::iterator vec_it;
60  typedef std::vector<std::string> name_vec;
61  typedef name_vec::iterator name_vec_it;
62  typedef std::pair<std::string, Entity_kind> key;
63  typedef std::map<key, vec> mymap;
64  typedef mymap::iterator map_it;
65 
67  map_it begin() { return state_vectors_.begin(); }
69  map_it end() { return state_vectors_.end(); }
70 
72  name_vec_it names_begin() { return names_.begin(); }
74  name_vec_it names_end() { return names_.end(); }
75 
78  name_vec names() const{return names_;}
79 
90  map_it find(std::string const name,
91  Entity_kind const on_what = Entity_kind::ANY_KIND) {
92  key testKey(name, on_what);
93  auto it = begin();
94  while (it != end()) {
95  if ((it->first == testKey) ||
96  ((it->first.first == name) &&
97  (on_what == Entity_kind::ANY_KIND)))
98  break;
99  else
100  ++it;
101  }
102  return it;
103  }
104 
120  vec& add(std::string const name, Entity_kind const on_what,
121  double const * const data = nullptr) {
122  auto it = find(name, on_what);
123  if (it == end()) {
124  // This is a new variable, so store it.
125  auto num_ent = mesh_->num_entities(on_what, Entity_type::PARALLEL_OWNED);
126  key thisKey(name, on_what);
127  if (data == nullptr) {
128  state_vectors_.insert(std::make_pair(thisKey,
129  vec(num_ent)));
130  } else {
131  state_vectors_.insert(std::make_pair(thisKey,
132  vec(data, data+num_ent)));
133  }
134  names_.emplace_back(name);
135  return state_vectors_[thisKey];
136  } else {
137  // This already exists!
138  std::cerr << "Attempted to add duplicate vectors. Ignoring."
139  << std::endl;
140  return it->second;
141  }
142  }
143 
158  vec& add(std::string const name, Entity_kind const on_what,
159  double const val) {
160  auto it = find(name, on_what);
161  if (it == end()) {
162  // This is a new variable, so store it.
163  auto num_ent = mesh_->num_entities(on_what, Entity_type::PARALLEL_OWNED);
164  key thisKey(name, on_what);
165  state_vectors_.insert(std::make_pair(thisKey, vec(num_ent, val)));
166  names_.emplace_back(name);
167  return state_vectors_[thisKey];
168  } else {
169  // This already exists!
170  std::cerr << "Attempted to add duplicate vectors. Ignoring."
171  << std::endl;
172  return it->second;
173  }
174  }
175 
186  vec& get(std::string const name, Entity_kind const on_what) {
187  auto it = find(name, on_what);
188  if (it != end()) {
189  return it->second;
190  } else {
191  throw std::runtime_error("Couldn't find the requested variable.");
192  }
193  }
194 
195  private:
197  std::shared_ptr<Simple_Mesh> mesh_;
199  mymap state_vectors_;
201  name_vec names_;
202 }; // Simple_State
203 
204 } // namespace Wonton
205 
206 #endif // WONTON_SIMPLE_STATE_H_
std::map< key, vec > mymap
Definition: simple_state.h:63
name_vec::iterator name_vec_it
Definition: simple_state.h:61
std::vector< std::string > name_vec
Definition: simple_state.h:60
A very light-weight state manager for a Simple_Mesh.
Definition: simple_state.h:43
std::vector< double > vec
Convenience types.
Definition: simple_state.h:58
Factorize a number N into D equal (or nearly equal) factors.
Definition: adaptive_refinement_mesh.h:31
vec::iterator vec_it
Definition: simple_state.h:59
std::pair< std::string, Entity_kind > key
Definition: simple_state.h:62
mymap::iterator map_it
Definition: simple_state.h:64
~Simple_State()
Destructor.
Definition: simple_state.h:52
name_vec names() const
Definition: simple_state.h:78
vec & add(std::string const name, Entity_kind const on_what, double const val)
Add a field to the state manager.
Definition: simple_state.h:158
vec & add(std::string const name, Entity_kind const on_what, double const *const data=nullptr)
Add a field to the state manager.
Definition: simple_state.h:120
map_it find(std::string const name, Entity_kind const on_what=Entity_kind::ANY_KIND)
Search for a specific key (name, Entity_kind) within the map of known fields and return an iterator t...
Definition: simple_state.h:90
map_it begin()
An iterator to the beginning of the field map.
Definition: simple_state.h:67
int numVars() const
The number of variables that live in the state manager.
Definition: simple_state.h:55
Entity_kind
The type of mesh entity.
Definition: wonton.h:81
name_vec_it names_end()
An iterator to the ending fo the variable names vector.
Definition: simple_state.h:74
Simple_State(std::shared_ptr< Simple_Mesh > mesh)
Constructor for creating a Simple_State.
Definition: simple_state.h:49
A very light-weight, simple mesh infrastructure.
map_it end()
An iterator to the ending of the field map.
Definition: simple_state.h:69
Definition: wonton.h:83
Definition: wonton.h:127
name_vec_it names_begin()
An iterator to the beginning of the variable names vector.
Definition: simple_state.h:72