https://mooseframework.inl.gov
DataIO.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // MOOSE includes
13 #include "ADReal.h"
14 #include "MooseTypes.h"
15 #include "HashMap.h"
16 #include "MooseError.h"
17 #include "RankTwoTensor.h"
18 #include "RankThreeTensor.h"
19 #include "RankFourTensor.h"
20 #include "ColumnMajorMatrix.h"
21 #include "UniqueStorage.h"
22 
23 #include "libmesh/parallel.h"
24 #include "libmesh/parameters.h"
25 #include "libmesh/numeric_vector.h"
26 
27 #ifdef LIBMESH_HAVE_CXX11_TYPE_TRAITS
28 #include <type_traits>
29 #endif
30 
31 // C++ includes
32 #include <string>
33 #include <vector>
34 #include <list>
35 #include <iostream>
36 #include <map>
37 #include <unordered_map>
38 #include <unordered_set>
39 #include <memory>
40 #include <optional>
41 
42 namespace libMesh
43 {
44 template <typename T>
45 class DenseMatrix;
46 template <typename T>
47 class DenseVector;
48 template <typename T>
49 class VectorValue;
50 template <typename T>
51 class TensorValue;
52 class Elem;
53 class Point;
54 }
55 
59 template <typename P>
60 inline void storeHelper(std::ostream & stream, P & data, void * context);
61 
65 template <typename P>
66 inline void storeHelper(std::ostream & stream, std::vector<P> & data, void * context);
67 
71 template <typename P>
72 inline void storeHelper(std::ostream & stream, std::shared_ptr<P> & data, void * context);
73 
77 template <typename P>
78 inline void storeHelper(std::ostream & stream, std::unique_ptr<P> & data, void * context);
79 
83 template <typename P>
84 inline void storeHelper(std::ostream & stream, std::set<P> & data, void * context);
85 
89 template <typename P, typename Q>
90 inline void storeHelper(std::ostream & stream, std::map<P, Q> & data, void * context);
91 
95 template <typename P, typename Q>
96 inline void storeHelper(std::ostream & stream, std::unordered_map<P, Q> & data, void * context);
97 
101 template <typename P>
102 inline void storeHelper(std::ostream & stream, std::optional<P> & data, void * context);
103 
107 template <typename P, typename Q>
108 inline void storeHelper(std::ostream & stream, HashMap<P, Q> & data, void * context);
109 
113 template <typename T>
114 inline void storeHelper(std::ostream & stream, UniqueStorage<T> & data, void * context);
115 
119 template <typename P>
120 inline void loadHelper(std::istream & stream, P & data, void * context);
121 
125 template <typename P>
126 inline void loadHelper(std::istream & stream, std::vector<P> & data, void * context);
127 
131 template <typename P>
132 inline void loadHelper(std::istream & stream, std::shared_ptr<P> & data, void * context);
133 
137 template <typename P>
138 inline void loadHelper(std::istream & stream, std::unique_ptr<P> & data, void * context);
139 
143 template <typename P>
144 inline void loadHelper(std::istream & stream, std::set<P> & data, void * context);
145 
149 template <typename P, typename Q>
150 inline void loadHelper(std::istream & stream, std::map<P, Q> & data, void * context);
151 
155 template <typename P, typename Q>
156 inline void loadHelper(std::istream & stream, std::unordered_map<P, Q> & data, void * context);
157 
161 template <typename P>
162 inline void loadHelper(std::istream & stream, std::optional<P> & data, void * context);
163 
167 template <typename P, typename Q>
168 inline void loadHelper(std::istream & stream, HashMap<P, Q> & data, void * context);
169 
173 template <typename T>
174 inline void loadHelper(std::istream & stream, UniqueStorage<T> & data, void * context);
175 
176 template <typename T>
177 inline void dataStore(std::ostream & stream, T & v, void * /*context*/);
178 
179 // DO NOT MODIFY THE NEXT LINE - It is used by MOOSEDocs
180 // *************** Global Store Declarations *****************
181 template <typename T>
182 inline void
183 dataStore(std::ostream & stream, T & v, void * /*context*/)
184 {
185 #ifdef LIBMESH_HAVE_CXX11_TYPE_TRAITS
186  static_assert(std::is_polymorphic<T>::value == false,
187  "Cannot serialize a class that has virtual "
188  "members!\nWrite a custom dataStore() "
189  "template specialization!\n\n");
190  static_assert(std::is_trivially_copyable<T>::value,
191  "Cannot serialize a class that is not trivially copyable!\nWrite a custom "
192  "dataStore() template specialization!\n\n");
193 #endif
194 
195  stream.write((char *)&v, sizeof(v));
196  mooseAssert(!stream.bad(), "Failed to store");
197 }
198 
199 template <typename T>
200 inline void
201 dataStore(std::ostream & /*stream*/, T *& /*v*/, void * /*context*/)
202 {
203  mooseError("Attempting to store a raw pointer type: \"",
204  libMesh::demangle(typeid(T).name()),
205  " *\" as restartable data!\nWrite a custom dataStore() template specialization!\n\n");
206 }
207 
208 void dataStore(std::ostream & stream, Point & p, void * context);
209 
210 template <typename T, typename U>
211 inline void
212 dataStore(std::ostream & stream, std::pair<T, U> & p, void * context)
213 {
214  storeHelper(stream, p.first, context);
215  storeHelper(stream, p.second, context);
216 }
217 
218 template <typename T>
219 inline void
220 dataStore(std::ostream & stream, std::vector<T> & v, void * context)
221 {
222  // First store the size of the vector
223  unsigned int size = v.size();
224  dataStore(stream, size, nullptr);
225 
226  for (unsigned int i = 0; i < size; i++)
227  storeHelper(stream, v[i], context);
228 }
229 
230 template <typename T>
231 inline void
232 dataStore(std::ostream & stream, std::shared_ptr<T> & v, void * context)
233 {
234  T * tmp = v.get();
235 
236  storeHelper(stream, tmp, context);
237 }
238 
239 template <typename T>
240 inline void
241 dataStore(std::ostream & stream, std::unique_ptr<T> & v, void * context)
242 {
243  T * tmp = v.get();
244 
245  storeHelper(stream, tmp, context);
246 }
247 
248 template <typename T>
249 inline void
250 dataStore(std::ostream & stream, std::set<T> & s, void * context)
251 {
252  // First store the size of the set
253  unsigned int size = s.size();
254  dataStore(stream, size, nullptr);
255 
256  typename std::set<T>::iterator it = s.begin();
257  typename std::set<T>::iterator end = s.end();
258 
259  for (; it != end; ++it)
260  {
261  T & x = const_cast<T &>(*it);
262  storeHelper(stream, x, context);
263  }
264 }
265 
266 template <typename T>
267 inline void
268 dataStore(std::ostream & stream, std::list<T> & l, void * context)
269 {
270  // First store the size of the set
271  unsigned int size = l.size();
272  dataStore(stream, size, nullptr);
273 
274  typename std::list<T>::iterator it = l.begin();
275  typename std::list<T>::iterator end = l.end();
276 
277  for (; it != end; ++it)
278  {
279  T & x = const_cast<T &>(*it);
280  storeHelper(stream, x, context);
281  }
282 }
283 
284 template <typename T>
285 inline void
286 dataStore(std::ostream & stream, std::deque<T> & l, void * context)
287 {
288  // First store the size of the container
289  unsigned int size = l.size();
290  dataStore(stream, size, nullptr);
291 
292  typename std::deque<T>::iterator it = l.begin();
293  typename std::deque<T>::iterator end = l.end();
294 
295  for (; it != end; ++it)
296  {
297  T & x = const_cast<T &>(*it);
298  storeHelper(stream, x, context);
299  }
300 }
301 
302 template <typename T, typename U>
303 inline void
304 dataStore(std::ostream & stream, std::map<T, U> & m, void * context)
305 {
306  // First store the size of the map
307  unsigned int size = m.size();
308  dataStore(stream, size, nullptr);
309 
310  typename std::map<T, U>::iterator it = m.begin();
311  typename std::map<T, U>::iterator end = m.end();
312 
313  for (; it != end; ++it)
314  {
315  T & key = const_cast<T &>(it->first);
316 
317  storeHelper(stream, key, context);
318 
319  storeHelper(stream, it->second, context);
320  }
321 }
322 
323 template <typename T, typename U>
324 inline void
325 dataStore(std::ostream & stream, std::unordered_map<T, U> & m, void * context)
326 {
327  // First store the size of the map
328  unsigned int size = m.size();
329  dataStore(stream, size, nullptr);
330 
331  typename std::unordered_map<T, U>::iterator it = m.begin();
332  typename std::unordered_map<T, U>::iterator end = m.end();
333 
334  for (; it != end; ++it)
335  {
336  T & key = const_cast<T &>(it->first);
337 
338  storeHelper(stream, key, context);
339 
340  storeHelper(stream, it->second, context);
341  }
342 }
343 
344 template <typename T>
345 inline void
346 dataStore(std::ostream & stream, std::unordered_set<T> & s, void * context)
347 {
348  // First store the size of the set
349  std::size_t size = s.size();
350  dataStore(stream, size, nullptr);
351 
352  for (auto & element : s)
353  dataStore(stream, element, context);
354 }
355 
356 template <typename T>
357 inline void
358 dataStore(std::ostream & stream, std::optional<T> & m, void * context)
359 {
360  bool has_value = m.has_value();
361  dataStore(stream, has_value, nullptr);
362 
363  if (has_value)
364  storeHelper(stream, *m, context);
365 }
366 
367 template <typename T, typename U>
368 inline void
369 dataStore(std::ostream & stream, HashMap<T, U> & m, void * context)
370 {
371  // First store the size of the map
372  unsigned int size = m.size();
373  dataStore(stream, size, nullptr);
374 
375  typename HashMap<T, U>::iterator it = m.begin();
376  typename HashMap<T, U>::iterator end = m.end();
377 
378  for (; it != end; ++it)
379  {
380  T & key = const_cast<T &>(it->first);
381 
382  storeHelper(stream, key, context);
383 
384  storeHelper(stream, it->second, context);
385  }
386 }
387 
388 template <typename T, int Rows, int Cols>
389 void
390 dataStore(std::ostream & stream, Eigen::Matrix<T, Rows, Cols> & v, void * context)
391 {
392  auto m = cast_int<unsigned int>(v.rows());
393  dataStore(stream, m, context);
394  auto n = cast_int<unsigned int>(v.cols());
395  dataStore(stream, n, context);
396  for (const auto i : make_range(m))
397  for (const auto j : make_range(n))
398  {
399  auto & r = v(i, j);
400  dataStore(stream, r, context);
401  }
402 }
403 
404 // Specializations (defined in .C)
405 template <>
406 void dataStore(std::ostream & stream, Real & v, void * context);
407 template <>
408 void dataStore(std::ostream & stream, std::string & v, void * context);
409 template <>
410 void dataStore(std::ostream & stream, VariableName & v, void * context);
411 template <>
412 void dataStore(std::ostream & stream, UserObjectName & v, void * context);
413 template <>
414 void dataStore(std::ostream & stream, bool & v, void * context);
415 // Vectors of bools are special
416 // https://en.wikipedia.org/w/index.php?title=Sequence_container_(C%2B%2B)&oldid=767869909#Specialization_for_bool
417 template <>
418 void dataStore(std::ostream & stream, std::vector<bool> & v, void * context);
419 template <>
420 void dataStore(std::ostream & stream, const Elem *& e, void * context);
421 template <>
422 void dataStore(std::ostream & stream, const Node *& n, void * context);
423 template <>
424 void dataStore(std::ostream & stream, Elem *& e, void * context);
425 template <>
426 void dataStore(std::ostream & stream, Node *& n, void * context);
427 template <>
428 void dataStore(std::ostream & stream, std::stringstream & s, void * context);
429 template <>
430 void dataStore(std::ostream & stream, ADReal & dn, void * context);
431 template <>
432 void dataStore(std::ostream & stream, libMesh::Parameters & p, void * context);
433 
434 template <>
445 void dataStore(std::ostream & stream,
446  std::unique_ptr<libMesh::NumericVector<libMesh::Number>> & v,
447  void * context);
448 
449 template <std::size_t N>
450 inline void
451 dataStore(std::ostream & stream, std::array<ADReal, N> & dn, void * context)
452 {
453  for (std::size_t i = 0; i < N; ++i)
454  dataStore(stream, dn[i], context);
455 }
456 
457 template <std::size_t N>
458 inline void
459 dataStore(std::ostream & stream, ADReal (&dn)[N], void * context)
460 {
461  for (std::size_t i = 0; i < N; ++i)
462  dataStore(stream, dn[i], context);
463 }
464 
465 template <typename T>
466 void
467 dataStore(std::ostream & stream, libMesh::NumericVector<T> & v, void * context)
468 {
469  v.close();
470 
471  numeric_index_type size = v.local_size();
472 
473  for (numeric_index_type i = v.first_local_index(); i < v.first_local_index() + size; i++)
474  {
475  T r = v(i);
476  dataStore(stream, r, context);
477  }
478 }
479 
480 template <>
481 void dataStore(std::ostream & stream, Vec & v, void * context);
482 
483 template <typename T>
484 void
485 dataStore(std::ostream & stream, DenseVector<T> & v, void * context)
486 {
487  unsigned int m = v.size();
488  dataStore(stream, m, nullptr);
489  for (unsigned int i = 0; i < v.size(); i++)
490  {
491  T r = v(i);
492  dataStore(stream, r, context);
493  }
494 }
495 
496 template <typename T>
497 void dataStore(std::ostream & stream, libMesh::TensorValue<T> & v, void * context);
498 
499 template <typename T>
500 void dataStore(std::ostream & stream, libMesh::DenseMatrix<T> & v, void * context);
501 
502 template <typename T>
503 void dataStore(std::ostream & stream, libMesh::VectorValue<T> & v, void * context);
504 
505 template <typename T>
506 void
507 dataStore(std::ostream & stream, RankTwoTensorTempl<T> & rtt, void * context)
508 {
509  dataStore(stream, rtt._coords, context);
510 }
511 
512 template <typename T>
513 void
514 dataStore(std::ostream & stream, RankThreeTensorTempl<T> & rtt, void * context)
515 {
516  dataStore(stream, rtt._vals, context);
517 }
518 
519 template <typename T>
520 void
521 dataStore(std::ostream & stream, RankFourTensorTempl<T> & rft, void * context)
522 {
523  dataStore(stream, rft._vals, context);
524 }
525 
526 template <typename T>
527 void
528 dataStore(std::ostream & stream, SymmetricRankTwoTensorTempl<T> & srtt, void * context)
529 {
530  dataStore(stream, srtt._vals, context);
531 }
532 
533 template <typename T>
534 void
535 dataStore(std::ostream & stream, SymmetricRankFourTensorTempl<T> & srft, void * context)
536 {
537  dataStore(stream, srft._vals, context);
538 }
539 
540 template <typename T>
541 void
542 dataStore(std::ostream & stream, ColumnMajorMatrixTempl<T> & cmm, void * context)
543 {
544  dataStore(stream, cmm._values, context);
545 }
546 
547 // DO NOT MODIFY THE NEXT LINE - It is used by MOOSEDocs
548 // *************** Global Load Declarations *****************
549 template <typename T>
550 inline void
551 dataLoad(std::istream & stream, T & v, void * /*context*/)
552 {
553  stream.read((char *)&v, sizeof(v));
554  mooseAssert(!stream.bad(), "Failed to load");
555 }
556 
557 template <typename T>
558 void
559 dataLoad(std::istream & /*stream*/, T *& /*v*/, void * /*context*/)
560 {
561  mooseError("Attempting to load a raw pointer type: \"",
562  libMesh::demangle(typeid(T).name()),
563  " *\" as restartable data!\nWrite a custom dataLoad() template specialization!\n\n");
564 }
565 
566 template <typename T, typename U>
567 inline void
568 dataLoad(std::istream & stream, std::pair<T, U> & p, void * context)
569 {
570  loadHelper(stream, p.first, context);
571  loadHelper(stream, p.second, context);
572 }
573 
574 template <typename T>
575 inline void
576 dataLoad(std::istream & stream, std::vector<T> & v, void * context)
577 {
578  // First read the size of the vector
579  unsigned int size = 0;
580  dataLoad(stream, size, nullptr);
581 
582  v.resize(size);
583 
584  for (unsigned int i = 0; i < size; i++)
585  loadHelper(stream, v[i], context);
586 }
587 
588 template <typename T>
589 inline void
590 dataLoad(std::istream & stream, std::shared_ptr<T> & v, void * context)
591 {
592  T * tmp = v.get();
593 
594  loadHelper(stream, tmp, context);
595 }
596 
597 template <typename T>
598 inline void
599 dataLoad(std::istream & stream, std::unique_ptr<T> & v, void * context)
600 {
601  T * tmp = v.get();
602 
603  loadHelper(stream, tmp, context);
604 }
605 
606 template <typename T>
607 inline void
608 dataLoad(std::istream & stream, std::set<T> & s, void * context)
609 {
610  // First read the size of the set
611  unsigned int size = 0;
612  dataLoad(stream, size, nullptr);
613 
614  for (unsigned int i = 0; i < size; i++)
615  {
616  T data;
617  loadHelper(stream, data, context);
618  s.insert(std::move(data));
619  }
620 }
621 
622 template <typename T>
623 inline void
624 dataLoad(std::istream & stream, std::list<T> & l, void * context)
625 {
626  // First read the size of the set
627  unsigned int size = 0;
628  dataLoad(stream, size, nullptr);
629 
630  for (unsigned int i = 0; i < size; i++)
631  {
632  T data;
633  loadHelper(stream, data, context);
634  l.push_back(std::move(data));
635  }
636 }
637 
638 template <typename T>
639 inline void
640 dataLoad(std::istream & stream, std::deque<T> & l, void * context)
641 {
642  // First read the size of the container
643  unsigned int size = 0;
644  dataLoad(stream, size, nullptr);
645 
646  for (unsigned int i = 0; i < size; i++)
647  {
648  T data;
649  loadHelper(stream, data, context);
650  l.push_back(std::move(data));
651  }
652 }
653 
654 template <typename T, typename U>
655 inline void
656 dataLoad(std::istream & stream, std::map<T, U> & m, void * context)
657 {
658  m.clear();
659 
660  // First read the size of the map
661  unsigned int size = 0;
662  dataLoad(stream, size, nullptr);
663 
664  for (unsigned int i = 0; i < size; i++)
665  {
666  T key;
667  loadHelper(stream, key, context);
668 
669  U & value = m[key];
670  loadHelper(stream, value, context);
671  }
672 }
673 
674 template <typename T, typename U>
675 inline void
676 dataLoad(std::istream & stream, std::unordered_map<T, U> & m, void * context)
677 {
678  m.clear();
679 
680  // First read the size of the map
681  unsigned int size = 0;
682  dataLoad(stream, size, nullptr);
683 
684  for (unsigned int i = 0; i < size; i++)
685  {
686  T key;
687  loadHelper(stream, key, context);
688 
689  U & value = m[key];
690  loadHelper(stream, value, context);
691  }
692 }
693 
694 template <typename T>
695 inline void
696 dataLoad(std::istream & stream, std::unordered_set<T> & s, void * context)
697 {
698  s.clear();
699 
700  // First read the size of the set
701  std::size_t size = 0;
702  dataLoad(stream, size, nullptr);
703  s.reserve(size);
704 
705  for (std::size_t i = 0; i < size; i++)
706  {
707  T element;
708  dataLoad(stream, element, context);
709  s.insert(element);
710  }
711 }
712 
713 template <typename T>
714 inline void
715 dataLoad(std::istream & stream, std::optional<T> & m, void * context)
716 {
717  bool has_value;
718  dataLoad(stream, has_value, nullptr);
719 
720  if (has_value)
721  {
722  m = T{};
723  loadHelper(stream, *m, context);
724  }
725  else
726  m.reset();
727 }
728 
729 template <typename T, typename U>
730 inline void
731 dataLoad(std::istream & stream, HashMap<T, U> & m, void * context)
732 {
733  // First read the size of the map
734  unsigned int size = 0;
735  dataLoad(stream, size, nullptr);
736 
737  for (unsigned int i = 0; i < size; i++)
738  {
739  T key;
740  loadHelper(stream, key, context);
741 
742  U & value = m[key];
743  loadHelper(stream, value, context);
744  }
745 }
746 
747 template <typename T, int Rows, int Cols>
748 void
749 dataLoad(std::istream & stream, Eigen::Matrix<T, Rows, Cols> & v, void * context)
750 {
751  unsigned int m = 0;
752  dataLoad(stream, m, context);
753  unsigned int n = 0;
754  dataLoad(stream, n, context);
755  v.resize(m, n);
756  for (const auto i : make_range(m))
757  for (const auto j : make_range(n))
758  {
759  T r{};
760  dataLoad(stream, r, context);
761  v(i, j) = r;
762  }
763 }
764 
765 // Specializations (defined in .C)
766 template <>
767 void dataLoad(std::istream & stream, Real & v, void * /*context*/);
768 template <>
769 void dataLoad(std::istream & stream, std::string & v, void * /*context*/);
770 template <>
771 void dataLoad(std::istream & stream, VariableName & v, void * /*context*/);
772 template <>
773 void dataLoad(std::istream & stream, UserObjectName & v, void * /*context*/);
774 template <>
775 void dataLoad(std::istream & stream, bool & v, void * /*context*/);
776 // Vectors of bools are special
777 // https://en.wikipedia.org/w/index.php?title=Sequence_container_(C%2B%2B)&oldid=767869909#Specialization_for_bool
778 template <>
779 void dataLoad(std::istream & stream, std::vector<bool> & v, void * /*context*/);
780 template <>
781 void dataLoad(std::istream & stream, const Elem *& e, void * context);
782 template <>
783 void dataLoad(std::istream & stream, const Node *& e, void * context);
784 template <>
785 void dataLoad(std::istream & stream, Elem *& e, void * context);
786 template <>
787 void dataLoad(std::istream & stream, Node *& e, void * context);
788 template <>
789 void dataLoad(std::istream & stream, std::stringstream & s, void * context);
790 template <>
791 void dataLoad(std::istream & stream, ADReal & dn, void * context);
792 template <>
793 void dataLoad(std::istream & stream, libMesh::Parameters & p, void * context);
794 template <>
812 void dataLoad(std::istream & stream,
813  std::unique_ptr<libMesh::NumericVector<libMesh::Number>> & v,
814  void * context);
815 
816 template <std::size_t N>
817 inline void
818 dataLoad(std::istream & stream, std::array<ADReal, N> & dn, void * context)
819 {
820  for (std::size_t i = 0; i < N; ++i)
821  dataLoad(stream, dn[i], context);
822 }
823 
824 template <std::size_t N>
825 inline void
826 dataLoad(std::istream & stream, ADReal (&dn)[N], void * context)
827 {
828  for (std::size_t i = 0; i < N; ++i)
829  dataLoad(stream, dn[i], context);
830 }
831 
832 template <typename T>
833 void
834 dataLoad(std::istream & stream, libMesh::NumericVector<T> & v, void * context)
835 {
836  numeric_index_type size = v.local_size();
837  for (numeric_index_type i = v.first_local_index(); i < v.first_local_index() + size; i++)
838  {
839  T r = 0;
840  dataLoad(stream, r, context);
841  v.set(i, r);
842  }
843  v.close();
844 }
845 
846 template <>
847 void dataLoad(std::istream & stream, Vec & v, void * context);
848 
849 template <typename T>
850 void
851 dataLoad(std::istream & stream, DenseVector<T> & v, void * context)
852 {
853  unsigned int n = 0;
854  dataLoad(stream, n, nullptr);
855  v.resize(n);
856  for (unsigned int i = 0; i < n; i++)
857  {
858  T r = 0;
859  dataLoad(stream, r, context);
860  v(i) = r;
861  }
862 }
863 
864 template <typename T>
865 void dataLoad(std::istream & stream, libMesh::TensorValue<T> & v, void * context);
866 
867 template <typename T>
868 void dataLoad(std::istream & stream, libMesh::DenseMatrix<T> & v, void * context);
869 
870 template <typename T>
871 void dataLoad(std::istream & stream, libMesh::VectorValue<T> & v, void * context);
872 
873 template <typename T>
874 void
875 dataLoad(std::istream & stream, RankTwoTensorTempl<T> & rtt, void * context)
876 {
877  dataLoad(stream, rtt._coords, context);
878 }
879 
880 template <typename T>
881 void
882 dataLoad(std::istream & stream, RankThreeTensorTempl<T> & rtt, void * context)
883 {
884  dataLoad(stream, rtt._vals, context);
885 }
886 
887 template <typename T>
888 void
889 dataLoad(std::istream & stream, RankFourTensorTempl<T> & rft, void * context)
890 {
891  dataLoad(stream, rft._vals, context);
892 }
893 
894 template <typename T>
895 void
896 dataLoad(std::istream & stream, SymmetricRankTwoTensorTempl<T> & rtt, void * context)
897 {
898  dataLoad(stream, rtt._vals, context);
899 }
900 
901 template <typename T>
902 void
903 dataLoad(std::istream & stream, SymmetricRankFourTensorTempl<T> & rft, void * context)
904 {
905  dataLoad(stream, rft._vals, context);
906 }
907 
908 template <typename T>
909 void
910 dataLoad(std::istream & stream, ColumnMajorMatrixTempl<T> & cmm, void * context)
911 {
912  dataLoad(stream, cmm._values, context);
913 }
914 
915 // Scalar Helper Function
916 template <typename P>
917 inline void
918 storeHelper(std::ostream & stream, P & data, void * context)
919 {
920  dataStore(stream, data, context);
921 }
922 
923 // Vector Helper Function
924 template <typename P>
925 inline void
926 storeHelper(std::ostream & stream, std::vector<P> & data, void * context)
927 {
928  dataStore(stream, data, context);
929 }
930 
931 // std::shared_ptr Helper Function
932 template <typename P>
933 inline void
934 storeHelper(std::ostream & stream, std::shared_ptr<P> & data, void * context)
935 {
936  dataStore(stream, data, context);
937 }
938 
939 // std::unique Helper Function
940 template <typename P>
941 inline void
942 storeHelper(std::ostream & stream, std::unique_ptr<P> & data, void * context)
943 {
944  dataStore(stream, data, context);
945 }
946 
947 // Set Helper Function
948 template <typename P>
949 inline void
950 storeHelper(std::ostream & stream, std::set<P> & data, void * context)
951 {
952  dataStore(stream, data, context);
953 }
954 
955 // Map Helper Function
956 template <typename P, typename Q>
957 inline void
958 storeHelper(std::ostream & stream, std::map<P, Q> & data, void * context)
959 {
960  dataStore(stream, data, context);
961 }
962 
963 // Unordered_map Helper Function
964 template <typename P, typename Q>
965 inline void
966 storeHelper(std::ostream & stream, std::unordered_map<P, Q> & data, void * context)
967 {
968  dataStore(stream, data, context);
969 }
970 
971 // Optional Helper Function
972 template <typename P>
973 inline void
974 storeHelper(std::ostream & stream, std::optional<P> & data, void * context)
975 {
976  dataStore(stream, data, context);
977 }
978 
979 // HashMap Helper Function
980 template <typename P, typename Q>
981 inline void
982 storeHelper(std::ostream & stream, HashMap<P, Q> & data, void * context)
983 {
984  dataStore(stream, data, context);
985 }
986 
993 template <typename T>
994 inline void
995 storeHelper(std::ostream & stream, UniqueStorage<T> & data, void * context)
996 {
997  std::size_t size = data.size();
998  dataStore(stream, size, nullptr);
999 
1000  for (const auto i : index_range(data))
1001  {
1002  mooseAssert(data.hasValue(i), "Data doesn't have a value");
1003  storeHelper(stream, data.pointerValue(i), context);
1004  }
1005 }
1006 
1007 // Scalar Helper Function
1008 template <typename P>
1009 inline void
1010 loadHelper(std::istream & stream, P & data, void * context)
1011 {
1012  dataLoad(stream, data, context);
1013 }
1014 
1015 // Vector Helper Function
1016 template <typename P>
1017 inline void
1018 loadHelper(std::istream & stream, std::vector<P> & data, void * context)
1019 {
1020  dataLoad(stream, data, context);
1021 }
1022 
1023 // std::shared_ptr Helper Function
1024 template <typename P>
1025 inline void
1026 loadHelper(std::istream & stream, std::shared_ptr<P> & data, void * context)
1027 {
1028  dataLoad(stream, data, context);
1029 }
1030 
1031 // Unique Pointer Helper Function
1032 template <typename P>
1033 inline void
1034 loadHelper(std::istream & stream, std::unique_ptr<P> & data, void * context)
1035 {
1036  dataLoad(stream, data, context);
1037 }
1038 
1039 // Set Helper Function
1040 template <typename P>
1041 inline void
1042 loadHelper(std::istream & stream, std::set<P> & data, void * context)
1043 {
1044  dataLoad(stream, data, context);
1045 }
1046 
1047 // Map Helper Function
1048 template <typename P, typename Q>
1049 inline void
1050 loadHelper(std::istream & stream, std::map<P, Q> & data, void * context)
1051 {
1052  dataLoad(stream, data, context);
1053 }
1054 
1055 // Unordered_map Helper Function
1056 template <typename P, typename Q>
1057 inline void
1058 loadHelper(std::istream & stream, std::unordered_map<P, Q> & data, void * context)
1059 {
1060  dataLoad(stream, data, context);
1061 }
1062 
1063 // Optional Helper Function
1064 template <typename P>
1065 inline void
1066 loadHelper(std::istream & stream, std::optional<P> & data, void * context)
1067 {
1068  dataLoad(stream, data, context);
1069 }
1070 
1071 // HashMap Helper Function
1072 template <typename P, typename Q>
1073 inline void
1074 loadHelper(std::istream & stream, HashMap<P, Q> & data, void * context)
1075 {
1076  dataLoad(stream, data, context);
1077 }
1078 
1086 template <typename T>
1087 inline void
1088 loadHelper(std::istream & stream, UniqueStorage<T> & data, void * context)
1089 {
1090  std::size_t size;
1091  dataLoad(stream, size, nullptr);
1092  data.resize(size);
1093 
1094  for (const auto i : index_range(data))
1095  loadHelper(stream, data.pointerValue(i), context);
1096 }
1097 
1098 void dataLoad(std::istream & stream, Point & p, void * context);
1099 
1100 #ifndef TIMPI_HAVE_STRING_PACKING
1101 
1107 namespace libMesh
1108 {
1109 namespace Parallel
1110 {
1111 template <typename T>
1112 class Packing<std::basic_string<T>>
1113 {
1114 public:
1115  static const unsigned int size_bytes = 4;
1116 
1117  typedef T buffer_type;
1118 
1119  static unsigned int get_string_len(typename std::vector<T>::const_iterator in)
1120  {
1121  unsigned int string_len = reinterpret_cast<const unsigned char &>(in[size_bytes - 1]);
1122  for (signed int i = size_bytes - 2; i >= 0; --i)
1123  {
1124  string_len *= 256;
1125  string_len += reinterpret_cast<const unsigned char &>(in[i]);
1126  }
1127  return string_len;
1128  }
1129 
1130  static unsigned int packed_size(typename std::vector<T>::const_iterator in)
1131  {
1132  return get_string_len(in) + size_bytes;
1133  }
1134 
1135  static unsigned int packable_size(const std::basic_string<T> & s, const void *)
1136  {
1137  return s.size() + size_bytes;
1138  }
1139 
1140  template <typename Iter>
1141  static void pack(const std::basic_string<T> & b, Iter data_out, const void *)
1142  {
1143  unsigned int string_len = b.size();
1144  for (unsigned int i = 0; i != size_bytes; ++i)
1145  {
1146  *data_out++ = (string_len % 256);
1147  string_len /= 256;
1148  }
1149 
1150  std::copy(b.begin(), b.end(), data_out);
1151  }
1152 
1153  static std::basic_string<T> unpack(typename std::vector<T>::const_iterator in, void *)
1154  {
1155  unsigned int string_len = get_string_len(in);
1156 
1157  std::ostringstream oss;
1158  for (unsigned int i = 0; i < string_len; ++i)
1159  oss << reinterpret_cast<const unsigned char &>(in[i + size_bytes]);
1160 
1161  in += size_bytes + string_len;
1162 
1163  return oss.str();
1164  }
1165 };
1166 
1167 } // namespace Parallel
1168 
1169 } // namespace libMesh
1170 
1171 #endif
std::string name(const ElemQuality q)
RankFourTensorTempl is designed to handle any N-dimensional fourth order tensor, C.
const std::unique_ptr< T > & pointerValue(const std::size_t i) const
Returns a read-only reference to the underlying unique pointer at index i.
std::array< T, N2 > _vals
The values of the rank-four tensor.
HashMap is an abstraction for dictionary data type, we make it thread-safe by locking inserts...
Definition: HashMap.h:18
This class defines a Tensor that can change its shape.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323
static std::basic_string< T > unpack(typename std::vector< T >::const_iterator in, void *)
Definition: DataIO.h:1153
Storage container that stores a vector of unique pointers of T, but represents most of the public fac...
Definition: UniqueStorage.h:18
static unsigned int packable_size(const std::basic_string< T > &s, const void *)
Definition: DataIO.h:1135
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
T _coords[LIBMESH_DIM *LIBMESH_DIM]
DualNumber< Real, DNDerivativeType, true > ADReal
Definition: ADRealForward.h:42
std::size_t size() const
RankThreeTensor is designed to handle any N-dimensional third order tensor, r.
void resize(const std::size_t size)
Resizes the underlying vector.
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:918
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
dof_id_type numeric_index_type
static unsigned int packed_size(typename std::vector< T >::const_iterator in)
Definition: DataIO.h:1130
SymmetricRankTwoTensorTempl is designed to handle the Stress or Strain Tensor for an anisotropic mate...
T _vals[N3]
The values of the rank-three tensor stored by index=((i * LIBMESH_DIM + j) * LIBMESH_DIM + k) ...
T _vals[N4]
The values of the rank-four tensor stored by index=(((i * LIBMESH_DIM + j) * LIBMESH_DIM + k) * LIBME...
std::string demangle(const char *name)
virtual void close()=0
void dataStore(std::ostream &stream, T &v, void *)
Definition: DataIO.h:183
static void pack(const std::basic_string< T > &b, Iter data_out, const void *)
Definition: DataIO.h:1141
virtual numeric_index_type first_local_index() const=0
RankTwoTensorTempl is designed to handle the Stress or Strain Tensor for a fully anisotropic material...
Definition: RankTwoTensor.h:87
std::vector< T > _values
virtual numeric_index_type local_size() const=0
SymmetricRankFourTensorTempl is designed to handle an N-dimensional fourth order tensor with minor sy...
IntRange< T > make_range(T beg, T end)
static unsigned int get_string_len(typename std::vector< T >::const_iterator in)
Definition: DataIO.h:1119
virtual void set(const numeric_index_type i, const T value)=0
void dataLoad(std::istream &stream, T &v, void *)
Definition: DataIO.h:551
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:1010
bool hasValue(const std::size_t i) const
auto index_range(const T &sizable)