flat_state_wrapper.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 FLAT_STATE_WRAPPER_H_
8 #define FLAT_STATE_WRAPPER_H_
9 
10 #include <map>
11 #include <memory>
12 #include <vector>
13 #include <stdexcept>
14 #include <utility>
15 #include <algorithm>
16 
17 #include "wonton/support/wonton.h"
18 #include "wonton/support/Point.h"
19 
25 namespace Wonton {
26 
33 template <class T=double>
34 class Flat_State_Wrapper {
35  public:
36 
38  using pair_t = std::pair<std::string, Entity_kind>;
39 
43  Flat_State_Wrapper() = default;
44 
60  template <class State_Wrapper>
61  void initialize(State_Wrapper const & input,
62  std::vector<std::string> const& var_names)
63  {
64  clear(); // forget everything
65 
66  for (auto varname : var_names)
67  {
68  // get name
69  // get entity
70  Entity_kind entity = input.get_entity(varname);
71 
72  // get pointer to data for state from input state wrapper
73  T const* data;
74  input.mesh_get_data(entity, varname, &data);
75 
76  // copy input state data into new vector storage
77  size_t dataSize = input.get_data_size(entity, varname);
78  auto vdata = std::make_shared<std::vector<T>>(dataSize);
79  std::copy(data, data+dataSize, vdata->begin());
80 
81  // add to database
82  mesh_add_data(entity, varname, vdata);
83  }
84  }
85 
90 
94  ~Flat_State_Wrapper() = default;
95 
110  void initialize(std::vector<std::string> const& names,
111  std::vector<Entity_kind> const& entities,
112  std::vector<std::shared_ptr<std::vector<T>>> const& data)
113  {
114  if (not (names.size() == entities.size() and names.size() == data.size() and data.size() == entities.size())) {
115  throw std::runtime_error("argument sizes do not agree");
116  }
117 
118  clear();
119 
120  for (size_t i=0; i<names.size(); i++) {
121  mesh_add_data(entities[i], names[i], data[i]);
122  }
123  }
124 
130  void get_names(Entity_kind on_what, std::vector<std::string>& names) {
131  names.clear();
132  for (auto iter=entity_map_.begin(); iter!=entity_map_.end(); iter++) {
133  if (iter->second == on_what) {
134  names.push_back(iter->first);
135  }
136  }
137  }
138 
143  int num_materials() const {
144  return 0;
145  }
146 
153  int mat_get_num_cells(int matid) const {
154  return 0;
155  }
156 
163  void mat_get_cells(int matid, std::vector<int> *matcells) const {
164  matcells->clear();
165  }
166 
173  int cell_get_num_mats(int cellid) const {
174  return 0;
175  }
176 
183  void cell_get_mats(int cellid, std::vector<int> *cellmats) const {
184  cellmats->clear();
185  }
186 
194  int cell_index_in_material(int meshcell, int matid) const {
195  return -1;
196  }
197 
205  Field_type field_type(Entity_kind on_what, std::string const& var_name)
206  const {
207  return Field_type::MESH_FIELD; // MULTI-MATERIAL FIELDS NOT IMPLEMENTED
208  }
209 
218  void mesh_get_data(Entity_kind on_what, std::string const& var_name,
219  T** data) {
220  pair_t pr(var_name, on_what);
221  auto iter = name_map_.find(pr);
222  if (iter != name_map_.end()) {
223  (*data) = (T*)(&((*(state_[iter->second]))[0]));
224  } else {
225  (*data) = nullptr;
226  }
227  }
228 
237  void mesh_get_data(Entity_kind on_what, std::string const& var_name,
238  T const **data) const {
239  pair_t pr(var_name, on_what);
240  auto iter = name_map_.find(pr);
241  if (iter != name_map_.end()) {
242  (*data) = (T const *)(&((*(state_[iter->second]))[0]));
243  } else {
244  (*data) = nullptr;
245  }
246  }
247 
255  void mat_get_celldata(std::string const& var_name, int matid,
256  T const **data) const {
257  }
258 
259 
274  void mat_get_celldata(std::string const& var_name, int matid, T **data) {
275  }
276 
277 
278 
290  Entity_kind get_entity(std::string const& var_name) const {
291  return entity_map_.at(var_name);
292  }
293 
299  Entity_kind get_entity(int index) const
300  {
301  return entities_.at(index);
302  }
303 
308  return entity_size_map_[ent];
309  }
310 
311 
317  const std::type_info& get_data_type(std::string const& name) const {
318  size_t index = -1;
319  pair_t name_cell_pair(name, Wonton::Entity_kind::CELL);
320  auto it = name_map_.find(name_cell_pair);
321  if (it != name_map_.end())
323  else {
324  pair_t name_node_pair = std::make_pair(name, Wonton::Entity_kind::NODE);
325  it = name_map_.find(name_node_pair);
326  if (it != name_map_.end())
328  }
329 
330  if (index >= 0) {
331  return typeid(T);
332  } else {
333  std::cerr << "Could not find state variable " << name << "\n";
334  return typeid(0);
335  }
336  }
337 
338 
342  size_t get_vector_index(Entity_kind ent, std::string const& name) {
343  pair_t pair(name, ent);
344  return name_map_[pair];
345  }
346 
350  std::shared_ptr<std::vector<T>> get_vector(size_t index)
351  {
352  return state_[index];
353  }
354 
358  std::shared_ptr<std::vector<Wonton::Point3>> get_gradients(size_t index)
359  {
360  return gradients_[index];
361  }
362 
366  size_t get_num_vectors() { return state_.size(); }
367 
371  void add_gradients(std::shared_ptr<std::vector<Wonton::Point3>> new_grad)
372  {
373  if (new_grad->empty()) return;
374  gradients_.push_back(new_grad);
375  }
376 
380  size_t get_field_stride(size_t index)
381  {
382  return 1;
383  }
384 
388  size_t get_num_gradients() { return gradients_.size(); }
389 
390 private:
391  std::vector<std::shared_ptr<std::vector<T>>> state_;
392  int nmats_ = 0;
393  std::map<pair_t, size_t> name_map_;
394  std::vector<Entity_kind> entities_;
395  std::map<std::string, Entity_kind> entity_map_;
396  std::map<Entity_kind, size_t> entity_size_map_;
397  std::vector<std::shared_ptr<std::vector<Wonton::Point3>>> gradients_;
398 
402  void clear(){
403  state_.clear();
404  name_map_.clear();
405  entity_map_.clear();
406  entity_size_map_.clear();
407  gradients_.clear();
408  }
409 
420  void mesh_add_data(Entity_kind on_what, std::string const& var_name,
421  std::shared_ptr<std::vector<T>> data) {
422  // if we have seen this entity before - check size match, else
423  // store this size
424  auto sziter = entity_size_map_.find(on_what);
425  if (sziter != entity_size_map_.end()) {
426  if (sziter->second != data->size()) {
427  throw std::runtime_error(std::string("variable ")+var_name+
428  " has incompatible size on add");
429  }
430  } else {
431  entity_size_map_[on_what] = data->size();
432  }
433 
434  // store data and update internal book-keeping
435  pair_t pair(var_name, on_what);
436  auto iter = name_map_.find(pair);
437  if (iter == name_map_.end()) { // have not seen this entity-name combo before, add data
438  state_.push_back(data);
439  name_map_[pair] = state_.size() - 1;
440  entities_.push_back(on_what);
441  entity_map_[var_name] = on_what;
442  entity_size_map_[on_what] = data->size();
443  } else { // have seen the entity-name combo already, replace data. already checked size.
444  std::copy(data->begin(), data->end(), state_[iter->second]->begin());
445  }
446  }
447 }; // Flat_State_Wrapper
448 
449 } // namespace Wonton
450 
451 #endif // FLAT_STATE_WRAPPER_H_
Entity_kind get_entity(int index) const
Get the entity type on which the given field is defined.
Definition: flat_state_wrapper.h:299
void cell_get_mats(int cellid, std::vector< int > *cellmats) const
Get the IDs of materials in a cell.
Definition: flat_state_wrapper.h:183
std::shared_ptr< std::vector< T > > get_vector(size_t index)
Get the data vector.
Definition: flat_state_wrapper.h:350
Factorize a number N into D equal (or nearly equal) factors.
Definition: adaptive_refinement_mesh.h:31
std::vector< T > vector
Definition: wonton.h:285
void initialize(std::vector< std::string > const &names, std::vector< Entity_kind > const &entities, std::vector< std::shared_ptr< std::vector< T >>> const &data)
Initialize the state wrapper with explicit lists of names, entities and data.
Definition: flat_state_wrapper.h:110
A state manager wrapper that allows redistribution of data across nodes.
Definition: flat_state_mm_wrapper.h:33
size_t get_num_gradients()
Get the number of gradient vectors.
Definition: flat_state_wrapper.h:388
void mat_get_celldata(std::string const &var_name, int matid, T const **data) const
Get pointer to read-only scalar cell data for a particular material.
Definition: flat_state_wrapper.h:255
Definition: wonton.h:85
int cell_get_num_mats(int cellid) const
Get number of materials contained in a cell.
Definition: flat_state_wrapper.h:173
void mesh_get_data(Entity_kind on_what, std::string const &var_name, T const **data) const
Get pointer to scalar data.
Definition: flat_state_wrapper.h:237
int mat_get_num_cells(int matid) const
Get number of cells containing a particular material.
Definition: flat_state_wrapper.h:153
void mesh_get_data(Entity_kind on_what, std::string const &var_name, T **data)
Get pointer to scalar data.
Definition: flat_state_wrapper.h:218
Entity_kind get_entity(std::string const &var_name) const
Get the entity type on which the given field is defined.
Definition: flat_state_wrapper.h:290
std::pair< std::string, Entity_kind > pair_t
pair of name and entity to be used as data key
Definition: flat_state_wrapper.h:38
Definition: wonton.h:88
Field_type
Field type - whether it is mesh field or multi-material field.
Definition: wonton.h:187
size_t get_vector_index(Entity_kind ent, std::string const &name)
Get index for entity and name.
Definition: flat_state_wrapper.h:342
size_t get_num_vectors()
Get the number of data vectors.
Definition: flat_state_wrapper.h:366
void initialize(State_Wrapper const &input, std::vector< std::string > const &var_names)
Initialize the state wrapper with another state wrapper and a list of names.
Definition: flat_state_wrapper.h:61
std::shared_ptr< std::vector< Wonton::Point3 > > get_gradients(size_t index)
Get gradients.
Definition: flat_state_wrapper.h:358
Entity_kind
The type of mesh entity.
Definition: wonton.h:81
void mat_get_cells(int matid, std::vector< int > *matcells) const
Get cell indices containing a particular material.
Definition: flat_state_wrapper.h:163
int cell_index_in_material(int meshcell, int matid) const
Get the local index of mesh cell in material cell list.
Definition: flat_state_wrapper.h:194
Field_type field_type(Entity_kind on_what, std::string const &var_name) const
Type of field (MESH_FIELD or MULTIMATERIAL_FIELD)
Definition: flat_state_wrapper.h:205
void get_names(Entity_kind on_what, std::vector< std::string > &names)
Get names of data fields associated with a given entity.
Definition: flat_state_wrapper.h:130
int num_materials() const
Number of materials in problem.
Definition: flat_state_wrapper.h:143
void mat_get_celldata(std::string const &var_name, int matid, T **data)
Get pointer to read-write scalar data for a particular material.
Definition: flat_state_wrapper.h:274
Flat_State_Wrapper()=default
Constructor of Flat_State_Wrapper.
~Flat_State_Wrapper()=default
Destructor.
std::vector< std::string > const names() const
Return the names registered by the state manager.
Definition: state_manager.h:79
size_t get_entity_size(Entity_kind ent)
Get size for entity.
Definition: flat_state_wrapper.h:307
const std::type_info & get_data_type(std::string const &name) const
Get the data type of the given field.
Definition: flat_state_wrapper.h:317
void add_gradients(std::shared_ptr< std::vector< Wonton::Point3 >> new_grad)
Add a gradient field.
Definition: flat_state_wrapper.h:371
size_t get_field_stride(size_t index)
Get field stride.
Definition: flat_state_wrapper.h:380
Flat_State_Wrapper & operator=(Flat_State_Wrapper const &)=delete
Assignment operator (disabled).