jali_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 #ifndef WONTON_STATE_JALI_JALI_STATE_WRAPPER_H_
7 #define WONTON_STATE_JALI_JALI_STATE_WRAPPER_H_
8 
9 #include <type_traits>
10 #include <string>
11 #include <vector>
12 #include <memory>
13 
14 // Jali includes
15 #include "Mesh.hh"
16 #include "JaliState.h"
17 
18 // wonton includes
19 #include "wonton/support/wonton.h"
20 
25 namespace Wonton {
26 
32  public:
37  explicit Jali_State_Wrapper(Jali::State & jali_state)
38  : jali_state_(jali_state) {}
39 
45  jali_state_(state.jali_state_) {}
46 
51 
56 
57 
61  void init_from_mesh() { jali_state_.init_from_mesh(); }
62 
66  void export_to_mesh() {jali_state_.export_to_mesh(); }
67 
68 
73  int num_materials() const {
74  return jali_state_.num_materials();
75  }
76 
81  std::string material_name(int matid) const {
82  assert(matid >= 0 && matid < num_materials());
83  return jali_state_.material_name(matid);
84  }
85 
92  int mat_get_num_cells(int matid) const {
93  assert(matid >= 0 && matid < num_materials());
94  return jali_state_.material_cells(matid).size();
95  }
96 
103  void mat_get_cells(int matid, std::vector<int> *matcells) const {
104  assert(matid >= 0 && matid < num_materials());
105  matcells->clear();
106  *matcells = jali_state_.material_cells(matid);
107  }
108 
115  int cell_get_num_mats(int cellid) const {
116  return jali_state_.num_cell_materials(cellid);
117  }
118 
125  void cell_get_mats(int cellid, std::vector<int> *cellmats) const {
126  cellmats->clear();
127  *cellmats = jali_state_.cell_materials(cellid);
128  }
129 
137  int cell_index_in_material(int meshcell, int matid) const {
138  return jali_state_.cell_index_in_material(meshcell, matid);
139  }
140 
148  Field_type field_type(Entity_kind on_what, std::string const& var_name) const {
149 
150  auto it = jali_state_.cbegin();
151  while (it != jali_state_.cend()) {
152  std::shared_ptr<Jali::StateVectorBase> bvec = *it;
153  if (bvec->name() == var_name &&
154  static_cast<Entity_kind>(bvec->entity_kind()) == on_what) {
155  if (bvec->type() == Jali::StateVector_type::UNIVAL)
156  return Field_type::MESH_FIELD;
157  else
159  }
160  it++;
161  }
162  throw std::runtime_error("cannot retrieve field type");
163  }
164 
165 
173  template <class T>
174  void mesh_get_data(Entity_kind on_what, std::string const& var_name,
175  T const **data) const {
176  Jali::UniStateVector<T, Jali::Mesh> vector;
177  if (jali_state_.get<T, Jali::Mesh, Jali::UniStateVector>(var_name,
178  jali_state_.mesh(),
179  (Jali::Entity_kind) on_what,
181  &vector)) {
182  *data = (T const *) vector.get_raw_data();
183  } else {
184  std::cerr << "Could not find state variable " << var_name << "\n";
185  *data = nullptr;
186  }
187  }
188 
189 
202  template <class T>
203  void mesh_get_data(Entity_kind on_what, std::string const& var_name,
204  T **data) {
205  using T1 = typename std::remove_const<T>::type;
206  Jali::UniStateVector<T1, Jali::Mesh> vector;
207  if (jali_state_.get<T1, Jali::Mesh, Jali::UniStateVector>(var_name,
208  jali_state_.mesh(),
209  (Jali::Entity_kind) on_what,
211  &vector)) {
212  *data = vector.get_raw_data();
213  } else {
214  std::cerr << "Could not find state variable " << var_name << "\n";
215  *data = nullptr;
216  }
217  }
218 
219 
228  template <class T>
229  void mat_get_celldata(std::string const& var_name, int matid,
230  T const **data) const {
231  Jali::MultiStateVector<T, Jali::Mesh> mmvector;
232  if (jali_state_.get<T, Jali::Mesh, Jali::MultiStateVector>(var_name,
233  jali_state_.mesh(),
236  &mmvector)) {
237  // data copy
238  *data = (T const *) mmvector.get_raw_data(matid);
239  } else {
240  std::cerr << "Could not find state variable " << var_name << "\n";
241  *data = nullptr;
242  }
243  }
244 
245 
261  template <class T>
262  void mat_get_celldata(std::string const& var_name, int matid, T **data) {
263  using T1 = typename std::remove_const<T>::type;
264 
265  Jali::MultiStateVector<T1, Jali::Mesh> mmvector;
266  if (jali_state_.get<T1, Jali::Mesh, Jali::MultiStateVector>(var_name,
267  jali_state_.mesh(),
270  &mmvector)) {
271  // data copy
272  *data = mmvector.get_raw_data(matid);
273  } else {
274  std::cerr << "Could not find state variable " << var_name << "\n";
275  *data = nullptr;
276  }
277  }
278 
279 
286  template <class T>
287  void mesh_add_data(Entity_kind on_what, std::string const& var_name,
288  T const * const values) {
289  jali_state_.add(var_name, jali_state_.mesh(), (Jali::Entity_kind) on_what,
290  Jali::Entity_type::ALL, values);
291  }
292 
319  template <class T>
320  typename std::enable_if<(!std::is_pointer<T>::value &&
321  !std::is_array<T>::value),
322  void>::type
323  mesh_add_data(Entity_kind on_what, std::string const& var_name,
324  const T value) {
325  // Compiler needs some help deducing template parameters here
326  jali_state_.add<T, Jali::Mesh, Jali::UniStateVector>(var_name,
327  jali_state_.mesh(),
328  (Jali::Entity_kind) on_what,
330  value);
331  }
332 
333 
365  template <class T>
366  typename std::enable_if<(!std::is_pointer<T>::value &&
367  !std::is_array<T>::value),
368  void>::type
369  mat_add_celldata(std::string const& var_name, T value) {
370  jali_state_.add<T, Jali::Mesh, Jali::MultiStateVector>(var_name,
371  jali_state_.mesh(),
374  value);
375  }
376 
377 
390  template <class T>
391  void mat_add_celldata(std::string const& var_name,
392  T const * const *values = nullptr,
394  jali_state_.add(var_name, jali_state_.mesh(), Jali::Entity_kind::CELL,
395  Jali::Entity_type::ALL, (Jali::Data_layout) layout, values);
396  }
397 
398 
410  template <class T>
411  void mat_add_celldata(std::string const& var_name, int matid,
412  T const * values) {
413  auto it = jali_state_.find<T, Jali::Mesh, Jali::MultiStateVector>(var_name,
414  jali_state_.mesh(),
417 
418  if (it == jali_state_.end()) {
419  Jali::MultiStateVector<T, Jali::Mesh>& mmvec =
420  jali_state_.add<T, Jali::Mesh, Jali::MultiStateVector>(var_name,
421  jali_state_.mesh(),
424  std::vector<T>& matdata = mmvec.get_matdata(matid);
425  int nmatcells = matdata.size();
426  matdata.assign(values, values+nmatcells);
427  } else {
428  Jali::MultiStateVector<T, Jali::Mesh>& mmvec =
429  *(std::dynamic_pointer_cast<
430  Jali::MultiStateVector<T, Jali::Mesh>>(*it));
431  std::vector<T>& matdata = mmvec.get_matdata(matid);
432  int nmatcells = matdata.size();
433  matdata.assign(values, values+nmatcells);
434  }
435  }
436 
437 
459  template <class T>
460  typename
461  std::enable_if<(!std::is_pointer<T>::value && !std::is_array<T>::value),
462  void>::type
463  mat_add_celldata(std::string const& var_name, int matid, T value) {
464  auto it = jali_state_.find<T, Jali::Mesh, Jali::MultiStateVector>(var_name,
465  jali_state_.mesh(),
468 
469  if (it == jali_state_.end()) {
470  Jali::MultiStateVector<T, Jali::Mesh>& mmvec =
471  jali_state_.add<T, Jali::Mesh, Jali::MultiStateVector>(var_name,
472  jali_state_.mesh(),
475  std::vector<T>& matdata = mmvec.get_matdata(matid);
476  int nmatcells = matdata.size();
477  matdata.assign(nmatcells, value);
478  } else {
479  Jali::MultiStateVector<T, Jali::Mesh>& mmvec =
480  *(std::dynamic_pointer_cast<
481  Jali::MultiStateVector<T, Jali::Mesh>>(*it));
482  std::vector<T>& matdata = mmvec.get_matdata(matid);
483  int nmatcells = matdata.size();
484  matdata.assign(nmatcells, value);
485  }
486  }
487 
488 
489 
496  void mat_add_cells(int matid, std::vector<int> const& newcells) {
497  jali_state_.add_cells_to_material(matid, newcells);
498  }
499 
500 
507  void mat_rem_cells(int matid, std::vector<int> const& delcells) {
508  jali_state_.rem_cells_from_material(matid, delcells);
509  }
510 
511 
524  void add_material(std::string const& matname,
525  std::vector<int> const& matcells) {
526  jali_state_.add_material(matname, matcells);
527  }
528 
535  Entity_kind get_entity(const std::string var_name) const {
536 
537  Jali::State::const_iterator it = jali_state_.find(var_name);
538  if (it != jali_state_.cend()) {
539  std::shared_ptr<Jali::StateVectorBase> vector = *it;
540  if (vector)
541  return (Entity_kind) vector->entity_kind();
542  }
543 
544  // At the moment we are using this as the API function to tell whether a
545  // state vector is defined. In particular we use this to test for the
546  // non existence of material centroids which is acceptable in VOF. We
547  // don't have a runtime check of MOF vs. VOF vs other, so I (DWS) have
548  // disabled the warning here. If a MOF problem doesn't have material
549  // centroids I'm guessing a warning will appear somewhere else.
550  if (var_name != "mat_centroids")
551  std::cerr << "Could not find state variable " << var_name << "\n";
552 
553  return UNKNOWN_KIND;
554  }
555 
556 
566  int get_data_size(Entity_kind on_what, std::string const& var_name) const {
567 
568  Jali::State::const_iterator it = jali_state_.find(var_name);
569  if (it != jali_state_.cend()) {
570  std::shared_ptr<Jali::StateVectorBase> vector = *it;
571  if (vector && on_what == (Entity_kind) vector->entity_kind()) {
572  if (vector->type() == Jali::StateVector_type::UNIVAL) {
573  auto uvec = std::dynamic_pointer_cast<
574  Jali::UniStateVectorBase<Jali::Mesh>>(vector);
575  if (uvec && uvec->domain() == jali_state_.mesh())
576  return uvec->size();
577  } else if (vector->type() == Jali::StateVector_type::MULTIVAL) {
578  auto mmvec = std::dynamic_pointer_cast<
579  Jali::MultiStateVectorBase<Jali::Mesh>>(vector);
580  if (mmvec && mmvec->domain() == jali_state_.mesh())
581  return (vector->size());
582  }
583  }
584  }
585 
586  std::cerr << "Could not find state variable " << var_name << "\n";
587  return 0;
588  }
589 
590 
596  const std::type_info& get_data_type(std::string const& var_name) const {
597 
598  Jali::State::const_iterator it =
599  jali_state_.find(var_name, jali_state_.mesh());
600  if (it != jali_state_.cend()) {
601  std::shared_ptr<Jali::StateVectorBase> vector = *it;
602  if (vector)
603  return vector->data_type();
604  }
605 
606  std::cerr << "Could not find state variable " << var_name << "\n";
607  return typeid(0);
608  }
609 
614  std::vector<std::string>::iterator names_begin() const {
615  return jali_state_.names_begin();
616  }
617 
622  std::vector<std::string>::iterator names_end() const {
623  return jali_state_.names_end();
624  }
625 
630  std::vector<std::string> names() const {
631  std::vector<std::string> result(jali_state_.names_begin(),
632  jali_state_.names_end());
633  return result;
634  }
635 
636 
640  typedef Jali::State::string_permutation string_permutation;
641 
647  string_permutation names_entity_begin(Entity_kind const on_what) const {
648  return jali_state_.names_entity_begin((Jali::Entity_kind)on_what);
649  }
650 
655  string_permutation names_entity_end(Entity_kind const on_what) const {
656  return jali_state_.names_entity_end((Jali::Entity_kind)on_what);
657  }
658 
659  private:
660  Jali::State & jali_state_;
661 }; // Jali_State_Wrapper
662 
663 } // namespace Wonton
664 
665 #endif // WONTON_STATE_JALI_JALI_STATE_WRAPPER_H_
void export_to_mesh()
Export fields to mesh file.
Definition: jali_state_wrapper.h:66
Definition: wonton.h:130
Jali_State_Wrapper & operator=(Jali_State_Wrapper const &)=delete
Assignment operator (disabled) - don&#39;t know how to implement (RVG)
Provides access to data stored in Jali_State.
Definition: jali_state_wrapper.h:31
Definition: wonton.h:84
int cell_index_in_material(int meshcell, int matid) const
Get the local index of mesh cell in material cell list.
Definition: jali_state_wrapper.h:137
void mat_get_cells(int matid, std::vector< int > *matcells) const
Get cell indices containing a particular material.
Definition: jali_state_wrapper.h:103
string_permutation names_entity_begin(Entity_kind const on_what) const
Begin iterator on vector names of specific entity type.
Definition: jali_state_wrapper.h:647
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
Jali_State_Wrapper(Jali::State &jali_state)
Constructor of Jali_State_Wrapper.
Definition: jali_state_wrapper.h:37
std::enable_if<(!std::is_pointer< T >::value &&!std::is_array< T >::value), void >::type mat_add_celldata(std::string const &var_name, int matid, T value)
Add a scalar multi-valued data field on cells and initialize one of its material data to a uniform va...
Definition: jali_state_wrapper.h:463
Jali_State_Wrapper(Jali_State_Wrapper &state)
Copy constructor of Jali_State_Wrapper - not a deep copy.
Definition: jali_state_wrapper.h:44
int mat_get_num_cells(int matid) const
Get number of cells containing a particular material.
Definition: jali_state_wrapper.h:92
void mat_add_cells(int matid, std::vector< int > const &newcells)
Add cells to material (or add material to cells)
Definition: jali_state_wrapper.h:496
Data_layout
Definition: wonton.h:209
std::enable_if<(!std::is_pointer< T >::value &&!std::is_array< T >::value), void >::type mat_add_celldata(std::string const &var_name, T value)
Add a scalar multi-valued data field on cells and initialize its material data to a single value...
Definition: jali_state_wrapper.h:369
std::vector< std::string >::iterator names_begin() const
Begin iterator on vector names.
Definition: jali_state_wrapper.h:614
void mat_add_celldata(std::string const &var_name, T const *const *values=nullptr, Data_layout layout=Data_layout::MATERIAL_CENTRIC)
Add a scalar multi-valued data field on cells and initialize its material data according to a 2D arra...
Definition: jali_state_wrapper.h:391
Jali::State::string_permutation string_permutation
Typedef for permutation iterator on vector of strings.
Definition: jali_state_wrapper.h:640
string_permutation names_entity_end(Entity_kind const on_what) const
End iterator on vector of names of specific entity type.
Definition: jali_state_wrapper.h:655
Definition: wonton.h:88
Field_type
Field type - whether it is mesh field or multi-material field.
Definition: wonton.h:187
std::vector< std::string > names() const
Vector of names.
Definition: jali_state_wrapper.h:630
const std::type_info & get_data_type(std::string const &var_name) const
Get the data type of the given field.
Definition: jali_state_wrapper.h:596
void mat_add_celldata(std::string const &var_name, int matid, T const *values)
Add a scalar multi-valued data field on cells and add data to one of its materials.
Definition: jali_state_wrapper.h:411
int get_data_size(Entity_kind on_what, std::string const &var_name) const
Get the data size for the given field.
Definition: jali_state_wrapper.h:566
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: jali_state_wrapper.h:229
Field_type field_type(Entity_kind on_what, std::string const &var_name) const
Type of field (MESH_FIELD or MULTIMATERIAL_FIELD)
Definition: jali_state_wrapper.h:148
Entity_kind
The type of mesh entity.
Definition: wonton.h:81
void mesh_add_data(Entity_kind on_what, std::string const &var_name, T const *const values)
Add a scalar single valued data field.
Definition: jali_state_wrapper.h:287
void cell_get_mats(int cellid, std::vector< int > *cellmats) const
Get the IDs of materials in a cell.
Definition: jali_state_wrapper.h:125
void mesh_get_data(Entity_kind on_what, std::string const &var_name, T **data)
Get a pointer to read-write single-valued data on the mesh.
Definition: jali_state_wrapper.h:203
void mat_rem_cells(int matid, std::vector< int > const &delcells)
Remove cells from material (or remove material from cells)
Definition: jali_state_wrapper.h:507
void init_from_mesh()
Initialize fields from mesh file.
Definition: jali_state_wrapper.h:61
void add_material(std::string const &matname, std::vector< int > const &matcells)
Add a material to state.
Definition: jali_state_wrapper.h:524
std::enable_if<(!std::is_pointer< T >::value &&!std::is_array< T >::value), void >::type mesh_add_data(Entity_kind on_what, std::string const &var_name, const T value)
Add a scalar single valued data field with uniform values.
Definition: jali_state_wrapper.h:323
int cell_get_num_mats(int cellid) const
Get number of materials contained in a cell.
Definition: jali_state_wrapper.h:115
Entity_kind get_entity(const std::string var_name) const
Get the entity type on which the given field is defined.
Definition: jali_state_wrapper.h:535
int num_materials() const
Number of materials in problem.
Definition: jali_state_wrapper.h:73
~Jali_State_Wrapper()
Empty destructor.
Definition: jali_state_wrapper.h:55
std::string material_name(int matid) const
Name of material.
Definition: jali_state_wrapper.h:81
std::vector< std::string >::iterator names_end() const
End iterator on vector names.
Definition: jali_state_wrapper.h:622
void mesh_get_data(Entity_kind on_what, std::string const &var_name, T const **data) const
Get a pointer to read-only single-valued data on the mesh.
Definition: jali_state_wrapper.h:174
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: jali_state_wrapper.h:262