v0.9.0
SeriesMultiIndices.cpp
Go to the documentation of this file.
1 /** \file SeriesMultiIndices.cpp
2  * \brief Data structures for time series and load series storage,
3  */
4 
5 /* MoFEM is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation, either version 3 of the License, or (at your
8  * option) any later version.
9  *
10  * MoFEM is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with MoFEM. If not, see <http://www.gnu.org/licenses/>
17  */
18 
19 namespace MoFEM {
20 
21 FieldSeries::FieldSeries(moab::Interface &moab, const EntityHandle _meshset)
22  : meshset(_meshset), tagName(NULL), tagNameSize(0), record_begin(false),
23  record_end(false) {
24 
25  Tag th_SeriesName;
26  rval = moab.tag_get_handle("_SeriesName", th_SeriesName);
28  rval = moab.tag_get_by_ptr(th_SeriesName, &meshset, 1,
29  (const void **)&tagName, &tagNameSize);
31 
32  const int def_val_len = 0;
33 
34  // time
35  std::string Tag_SeriesTime = "_SeriesTime_" + getName();
36  double def_time = 0;
37  rval = moab.tag_get_handle(Tag_SeriesTime.c_str(), 1, MB_TYPE_DOUBLE,
38  th_SeriesTime, MB_TAG_CREAT | MB_TAG_SPARSE,
39  &def_time);
41 
42  // handles
43  std::string Tag_DataHandles_SeriesName = "_SeriesDataHandles_" + getName();
44  rval = moab.tag_get_handle(
45  Tag_DataHandles_SeriesName.c_str(), def_val_len, MB_TYPE_HANDLE,
46  th_SeriesDataHandles, MB_TAG_CREAT | MB_TAG_SPARSE | MB_TAG_VARLEN, NULL);
48 
49  // uids
50  std::string Tag_DataUIDs_SeriesName = "_SeriesDataUIDs_" + getName();
51  rval = moab.tag_get_handle(
52  Tag_DataUIDs_SeriesName.c_str(), def_val_len, MB_TYPE_OPAQUE,
54  MB_TAG_CREAT | MB_TAG_SPARSE | MB_TAG_BYTES | MB_TAG_VARLEN, NULL);
56 
57  // data
58  std::string Tag_Data_SeriesName = "_SeriesData_" + getName();
59  rval = moab.tag_get_handle(
60  Tag_Data_SeriesName.c_str(), def_val_len, MB_TYPE_OPAQUE, th_SeriesData,
61  MB_TAG_CREAT | MB_TAG_SPARSE | MB_TAG_BYTES | MB_TAG_VARLEN, NULL);
63 }
64 
66  int &nb_steps) const {
68  CHKERR moab.num_contained_meshsets(meshset, &nb_steps);
70 }
71 
73  const FieldData val) {
75  if (!record_begin) {
76  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
77  "you neet to set recording");
78  }
79  handles.push_back(ent);
80  uids.push_back(uid);
81  data.push_back(val);
83 }
84 
87  if (record_begin) {
88  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
89  "recording already begin");
90  }
91  record_begin = true;
93 }
94 
97  if (!record_begin) {
98  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
99  "recording not begin it can not be ended");
100  }
101  record_begin = false;
102  record_end = true;
103  ia.push_back(uids.size());
104  time.push_back(t);
106 }
107 
108 MoFEMErrorCode FieldSeries::read(moab::Interface &moab) {
110 
111  if (record_begin) {
112  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
113  "all series data will be lost");
114  }
115  if (record_end) {
116  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
117  "all series data will be lost");
118  }
119 
120  std::vector<EntityHandle> contained;
121  CHKERR moab.get_contained_meshsets(meshset, contained);
122  ia.resize(0);
123  time.resize(0);
124  handles.resize(0);
125  uids.resize(0);
126  data.resize(0);
127  ia.push_back(0);
128 
129  for (unsigned int mm = 0; mm < contained.size(); mm++) {
130  // time
131  {
132  double t;
133  CHKERR moab.tag_set_data(th_SeriesTime, &meshset, 1, &t);
134  time.push_back(t);
135  }
136  // handles
137  {
138  const EntityHandle *tag_data;
139  int tag_size;
140  CHKERR moab.tag_get_by_ptr(th_SeriesDataHandles, &meshset, 1,
141  (const void **)&tag_data, &tag_size);
142  handles.insert(handles.end(), tag_data, &tag_data[tag_size]);
143  }
144  // uids
145  {
146  const ShortId *tag_data;
147  int tag_size;
148  CHKERR moab.tag_get_by_ptr(th_SeriesDataUIDs, &meshset, 1,
149  (const void **)&tag_data, &tag_size);
150  int nb = tag_size / sizeof(ShortId);
151  uids.insert(uids.end(), tag_data, &tag_data[nb]);
152  }
153  if (handles.size() != uids.size()) {
154  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "data inconsistency");
155  }
156  // data
157  {
158  const FieldData *tag_data;
159  int tag_size;
160  CHKERR moab.tag_get_by_ptr(th_SeriesData, &meshset, 1,
161  (const void **)&tag_data, &tag_size);
162  int nb = tag_size / sizeof(FieldData);
163  data.insert(data.end(), tag_data, &tag_data[nb]);
164  }
165  if (data.size() != uids.size()) {
166  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "data inconsistency");
167  }
168  ia.push_back(data.size());
169  }
170 
172 }
173 
174 MoFEMErrorCode FieldSeries::save(moab::Interface &moab) const {
176 
177  if (record_begin) {
178  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "switch off recording");
179  }
180  if (!record_end) {
181  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "finish recording");
182  }
183 
184  std::vector<EntityHandle> contained;
185  CHKERR moab.get_contained_meshsets(meshset, contained);
186  unsigned int nb_contained = contained.size();
187  if (nb_contained < ia.size() - 1) {
188  contained.resize(ia.size());
189  }
190  for (unsigned int mm = ia.size() - 1; mm < nb_contained; mm++) {
191  CHKERR moab.remove_entities(meshset, &contained[mm], 1);
192  CHKERR moab.delete_entities(&contained[mm], 1);
193  }
194  for (unsigned int mm = nb_contained; mm < ia.size() - 1; mm++) {
195  EntityHandle new_meshset;
196  CHKERR moab.create_meshset(MESHSET_SET | MESHSET_TRACK_OWNER, new_meshset);
197  CHKERR moab.add_entities(meshset, &new_meshset, 1);
198  }
199  contained.resize(0);
200  CHKERR moab.get_contained_meshsets(meshset, contained);
201  if (contained.size() != ia.size() - 1) {
202  SETERRQ2(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
203  "data inconsistency nb_contained != ia.size()-1 %d!=%d",
204  contained.size(), ia.size() - 1);
205  }
206 
207  // time
208  for (unsigned int ii = 1; ii < ia.size(); ii++) {
209  CHKERR moab.tag_set_data(th_SeriesTime, &contained[ii - 1], 1,
210  &time[ii - 1]);
211  }
212 
213  // handles
214  for (unsigned int ii = 1; ii < ia.size(); ii++) {
215  void const *tag_data[] = {&handles[ia[ii - 1]]};
216  int tag_sizes[] = {(ia[ii] - ia[ii - 1])};
217  CHKERR moab.tag_set_by_ptr(th_SeriesDataHandles, &contained[ii - 1], 1,
218  tag_data, tag_sizes);
219  }
220  // uids
221  for (unsigned int ii = 1; ii < ia.size(); ii++) {
222  void const *tag_data[] = {&uids[ia[ii - 1]]};
223  int tag_sizes[] = {(ia[ii] - ia[ii - 1]) * (int)sizeof(ShortId)};
224  CHKERR moab.tag_set_by_ptr(th_SeriesDataUIDs, &contained[ii - 1], 1,
225  tag_data, tag_sizes);
226  }
227 
228  // data
229  for (unsigned int ii = 1; ii < ia.size(); ii++) {
230  void const *tag_data[] = {&data[ia[ii - 1]]};
231  int tag_sizes[] = {(ia[ii] - ia[ii - 1]) * (int)sizeof(FieldData)};
232  CHKERR moab.tag_set_by_ptr(th_SeriesData, &contained[ii - 1], 1, tag_data,
233  tag_sizes);
234  }
235 
237 }
238 
239 FieldSeriesStep::FieldSeriesStep(moab::Interface &moab,
240  const FieldSeries *_FieldSeries_ptr,
241  const int _step_number)
242  : interface_FieldSeries<FieldSeries>(_FieldSeries_ptr),
243  step_number(_step_number) {
244 
245  ierr = get_time_init(moab);
246  CHKERRABORT(PETSC_COMM_WORLD, ierr);
247 }
248 
249 MoFEMErrorCode FieldSeriesStep::get(moab::Interface &moab,
250  DofEntity_multiIndex &dofsField) const {
252 
253  std::vector<EntityHandle> contained;
254  CHKERR moab.get_contained_meshsets(ptr->meshset, contained);
255  if (contained.size() <= (unsigned int)step_number) {
256  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "data inconsistency");
257  }
258 
259  EntityHandle *handles_ptr;
260  int handles_size;
261  CHKERR moab.tag_get_by_ptr(ptr->th_SeriesDataHandles, &contained[step_number],
262  1, (const void **)&handles_ptr, &handles_size);
263 
264  ShortId *uids_ptr;
265  int uids_size;
266  CHKERR moab.tag_get_by_ptr(ptr->th_SeriesDataUIDs, &contained[step_number], 1,
267  (const void **)&uids_ptr, &uids_size);
268  uids_size /= sizeof(ShortId);
269 
270  if (handles_size != uids_size) {
271  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "data inconsistency");
272  }
273 
274  FieldData *data_ptr;
275  int data_size;
276  CHKERR moab.tag_get_by_ptr(ptr->th_SeriesData, &contained[step_number], 1,
277  (const void **)&data_ptr, &data_size);
278  data_size /= sizeof(FieldData);
279 
280  if (data_size != uids_size) {
281  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "data inconsistency");
282  }
283 
284  typedef multi_index_container<
285  boost::shared_ptr<DofEntity>,
286  indexed_by<
287 
288  hashed_non_unique<
289  tag<Composite_Ent_and_ShortId_mi_tag>,
290  composite_key<
291  DofEntity,
292  const_mem_fun<DofEntity, EntityHandle, &DofEntity::getEnt>,
293  const_mem_fun<DofEntity, ShortId,
295 
296  >>
297  DofEntity_multiIndex_short_uid_view;
298 
299  DofEntity_multiIndex_short_uid_view short_uid_view;
300  short_uid_view.insert(dofsField.begin(), dofsField.end());
301 
302  for (int ii = 0; ii < uids_size; ii++) {
303  EntityHandle ent = handles_ptr[ii];
304  ShortId uid = uids_ptr[ii];
305  FieldData val = data_ptr[ii];
306 
307  auto dit = short_uid_view.find(boost::make_tuple(ent, uid));
308  if (dit != short_uid_view.end()) {
309  (*dit)->getFieldData() = val;
310  } else {
311  SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND,
312  "data inconsistency, getting data series, dof on ENTITY and "
313  "ShortId can't be found");
314  }
315  }
316 
318 }
319 
322 
323  std::vector<EntityHandle> contained;
324  CHKERR moab.get_contained_meshsets(ptr->meshset, contained);
325  if (contained.size() <= (unsigned int)step_number) {
326  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "data inconsistency");
327  }
328  double *time_ptr;
329  int size;
330  CHKERR moab.tag_get_by_ptr(ptr->th_SeriesTime, &contained[step_number], 1,
331  (const void **)&time_ptr, &size);
332  time = *time_ptr;
334 }
335 
336 std::ostream &operator<<(std::ostream &os, const FieldSeries &e) {
337  os << "name " << e.getName() << " meshset " << e.getMeshset();
338  return os;
339 }
340 
341 std::ostream &operator<<(std::ostream &os, const FieldSeriesStep &e) {
342  os << *(e.get_FieldSeries_ptr()) << " step number " << e.step_number
343  << " time " << e.get_time();
344  return os;
345 }
346 
347 } // namespace MoFEM
Structure for recording (time) series.
MoFEMErrorCode get_time_init(Interface &moab)
const FieldSeries * get_FieldSeries_ptr() const
std::vector< FieldData > data
ShortId getNonNonuniqueShortId() const
get short uid it is unique in combination with entity handle
keeps information about DOF on the entity
MoFEMErrorCode push_dofs(const EntityHandle ent, const ShortId uid, const FieldData val)
MoFEMErrorCode save(Interface &moab) const
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:477
std::vector< double > time
std::vector< int > ia
#define MOAB_THROW(a)
Check error code of MoAB function and throw MoFEM exception.
Definition: definitions.h:602
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
EntityHandle getMeshset() const
get meshset
static MoFEMErrorCodeGeneric< moab::ErrorCode > rval
Definition: Exceptions.hpp:84
std::ostream & operator<<(std::ostream &os, const DataForcesAndSourcesCore::EntData &e)
FieldSeriesStep(Interface &moab, const FieldSeries *_FieldSeries_ptr, const int _step_number)
std::string getName() const
get series name
multi_index_container< boost::shared_ptr< DofEntity >, indexed_by< ordered_unique< tag< Unique_mi_tag >, member< DofEntity, UId, &DofEntity::globalUId > >, ordered_non_unique< tag< Composite_Name_And_Ent_And_EntDofIdx_mi_tag >, composite_key< DofEntity, const_mem_fun< DofEntity::interface_type_Field, boost::string_ref, &DofEntity::getNameRef >, const_mem_fun< DofEntity, EntityHandle, &DofEntity::getEnt >, const_mem_fun< DofEntity, DofIdx, &DofEntity::getEntDofIdx > > >, ordered_non_unique< tag< Unique_Ent_mi_tag >, const_mem_fun< DofEntity, const UId &, &DofEntity::getEntGlobalUniqueId > >, ordered_non_unique< tag< FieldName_mi_tag >, const_mem_fun< DofEntity::interface_type_Field, boost::string_ref, &DofEntity::getNameRef > >, ordered_non_unique< tag< Ent_mi_tag >, const_mem_fun< DofEntity, EntityHandle, &DofEntity::getEnt > >, ordered_non_unique< tag< Composite_Name_And_Ent_mi_tag >, composite_key< DofEntity, const_mem_fun< DofEntity::interface_type_Field, boost::string_ref, &DofEntity::getNameRef >, const_mem_fun< DofEntity, EntityHandle, &DofEntity::getEnt > > >, ordered_non_unique< tag< Composite_Name_And_Type_mi_tag >, composite_key< DofEntity, const_mem_fun< DofEntity::interface_type_Field, boost::string_ref, &DofEntity::getNameRef >, const_mem_fun< DofEntity::interface_type_RefEntity, EntityType, &DofEntity::getEntType > > > > > DofEntity_multiIndex
MultiIndex container keeps DofEntity.
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:66
std::vector< EntityHandle > handles
MoFEMErrorCode begin()
std::vector< ShortId > uids
MoFEMErrorCode read(Interface &moab)
#define CHKERR
Inline error check.
Definition: definitions.h:596
double FieldData
Field data type.
Definition: Types.hpp:36
int ShortId
Unique Id in the field.
Definition: Types.hpp:42
int tagNameSize
number of bits necessary to keep field series
FieldSeries(Interface &moab, const EntityHandle _meshset)
Structure for keeping time and step.
MoFEMErrorCode get_nb_steps(Interface &moab, int &nb_setps) const
const void * tagName
tag keeps name of the series
MoFEMErrorCode end(double time=0)
MoFEMErrorCode get(Interface &moab, DofEntity_multiIndex &dofsField) const
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:407