https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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#include "TwoVector.h"
23
24#include "libmesh/parallel.h"
25#include "libmesh/parameters.h"
26#include "libmesh/numeric_vector.h"
27#include "libmesh/enum_elem_quality.h"
28#include "libmesh/enum_elem_type.h"
29#include "libmesh/enum_inf_map_type.h"
30
31#include "LibtorchUtils.h"
32
33#ifdef LIBMESH_HAVE_CXX11_TYPE_TRAITS
34#include <type_traits>
35#endif
36
37// C++ includes
38#include <string>
39#include <vector>
40#include <list>
41#include <iostream>
42#include <map>
43#include <unordered_map>
44#include <unordered_set>
45#include <memory>
46#include <optional>
47
48#include "mtwist.h"
49
50namespace libMesh
51{
52template <typename T>
53class DenseMatrix;
54template <typename T>
55class DenseVector;
56template <typename T>
57class VectorValue;
58template <typename T>
59class TensorValue;
60class Elem;
61class FEType;
62class Point;
63class BoundingBox;
64}
65
66#ifdef MOOSE_MFEM_ENABLED
67namespace Moose::MFEM
68{
70{
71};
72}
73#endif
74template <typename T>
75inline void dataStore(std::ostream & stream, T & v, void * /*context*/);
76
77// DO NOT MODIFY THE NEXT LINE - It is used by MOOSEDocs
78// *************** Global Store Declarations *****************
79template <typename T>
80inline void
81dataStore(std::ostream &, T &, void * /*context*/)
82{
83 static_assert(sizeof(T) == 0, "You must write a dataStore specialization for your type");
84}
85
86namespace moose
87{
88namespace internal
89{
90template <typename T>
91void
92dataStoreSizeof(std::ostream & stream, T & v)
93{
94 stream.write((char *)&v, sizeof(v));
95 mooseAssert(!stream.bad(), "Failed to store");
96}
97}
98}
99
100// clang-format off
101#define dataStoreSizeofSpecialization(Type) \
102template <> \
103inline void \
104dataStore(std::ostream & stream, Type & v, void *) \
105{ \
106 moose::internal::dataStoreSizeof(stream, v); \
107} \
108template <> \
109inline void \
110dataStore(std::ostream & stream, const Type & v, void *) \
111{ \
112 moose::internal::dataStoreSizeof(stream, v); \
113}
114
115dataStoreSizeofSpecialization(bool)
116dataStoreSizeofSpecialization(char)
117dataStoreSizeofSpecialization(signed char)
118dataStoreSizeofSpecialization(short int)
119dataStoreSizeofSpecialization(int)
120dataStoreSizeofSpecialization(long int)
121dataStoreSizeofSpecialization(long long int)
122dataStoreSizeofSpecialization(unsigned char)
123dataStoreSizeofSpecialization(short unsigned int)
124dataStoreSizeofSpecialization(unsigned int)
125dataStoreSizeofSpecialization(long unsigned int)
126dataStoreSizeofSpecialization(long long unsigned int)
127dataStoreSizeofSpecialization(float)
128dataStoreSizeofSpecialization(double)
129dataStoreSizeofSpecialization(long double)
130
131#define dataStoreEnum(EnumType, IntType) \
132template <> \
133inline void dataStore(std::ostream & stream, EnumType & enum_type, void * ctx) \
134{ \
135 auto stored = static_cast<IntType>(enum_type); \
136 dataStore(stream, stored, ctx); \
137}
138
139dataStoreEnum(libMesh::InfMapType, int)
140dataStoreEnum(libMesh::FEFamily, int)
141dataStoreEnum(libMesh::ParallelType, int)
142dataStoreEnum(Moose::CoordinateSystemType, int)
143dataStoreEnum(libMesh::ElemQuality, int)
144dataStoreEnum(libMesh::ElemType, int)
145dataStoreEnum(libMesh::ElemMappingType, unsigned char)
146
147template <>
148void dataStore(std::ostream &, mt_state &, void *);
149// clang-format on
150template <>
151void dataStore(std::ostream & stream, libMesh::Point & p, void * context);
152template <>
153void dataStore(std::ostream & stream, const libMesh::Point & p, void * context);
154// Specializations (defined in .C)
155template <>
156void dataStore(std::ostream & stream, std::string & v, void * context);
157template <>
158void dataStore(std::ostream & stream, VariableName & v, void * context);
159template <>
160void dataStore(std::ostream & stream, UserObjectName & v, void * context);
161template <>
162void dataStore(std::ostream & stream, libMesh::FEType & v, void * context);
163template <>
164void dataStore(std::ostream & stream, const Elem *& e, void * context);
165template <>
166void dataStore(std::ostream & stream, const Node *& n, void * context);
167template <>
168void dataStore(std::ostream & stream, Elem *& e, void * context);
169template <>
170void dataStore(std::ostream & stream, Node *& n, void * context);
171template <>
172void dataStore(std::ostream & stream, std::stringstream & s, void * context);
173template <>
174void dataStore(std::ostream & stream, ADReal & dn, void * context);
175template <>
176void dataStore(std::ostream & stream, libMesh::Parameters & p, void * context);
177
178template <typename T>
179void dataStore(std::ostream & stream, std::shared_ptr<T> & v, void * context);
180
181template <typename T>
182void dataStore(std::ostream & stream, std::unique_ptr<T> & v, void * context);
183
184template <>
195void dataStore(std::ostream & stream,
196 std::unique_ptr<libMesh::NumericVector<libMesh::Number>> & v,
197 void * context);
198
199template <typename T>
200void dataStore(std::ostream & stream, libMesh::TensorValue<T> & v, void * context);
201
202template <typename T>
203void dataStore(std::ostream & stream, libMesh::DenseMatrix<T> & v, void * context);
204
205template <typename T>
206void dataStore(std::ostream & stream, libMesh::DenseVector<T> & v, void * context);
207
208template <typename T>
209void dataStore(std::ostream & stream, libMesh::VectorValue<T> & v, void * context);
210
211template <typename T>
212void dataStore(std::ostream & stream, std::vector<T> & v, void * context);
213
214template <typename T>
215void dataStore(std::ostream & stream, std::set<T> & s, void * context);
216
217template <typename T>
218void dataStore(std::ostream & stream, std::list<T> & l, void * context);
219
220template <typename T>
221void dataStore(std::ostream & stream, std::deque<T> & l, void * context);
222
223template <typename T, typename U>
224void dataStore(std::ostream & stream, std::map<T, U> & m, void * context);
225
226template <typename T, typename U>
227void dataStore(std::ostream & stream, std::unordered_map<T, U> & m, void * context);
228
229template <typename T>
230void dataStore(std::ostream & stream, std::unordered_set<T> & s, void * context);
231
232template <typename T, std::size_t N>
233void dataStore(std::ostream & stream, std::array<T, N> & arr, void * context);
234
235template <typename T, int Rows, int Cols>
236void dataStore(std::ostream & stream, Eigen::Matrix<T, Rows, Cols> & v, void * context);
237
238template <typename T>
239inline void
240dataStore(std::ostream & /*stream*/, T *& /*v*/, void * /*context*/)
241{
242 mooseError("Attempting to store a raw pointer type: \"",
243 libMesh::demangle(typeid(T).name()),
244 " *\" as restartable data!\nWrite a custom dataStore() template specialization!\n\n");
245}
246// clang-format on
247
248void dataStore(std::ostream & stream, libMesh::BoundingBox & p, void * context);
249
250template <typename T, typename U>
251inline void
252dataStore(std::ostream & stream, std::pair<T, U> & p, void * context)
253{
254 dataStore(stream, p.first, context);
255 dataStore(stream, p.second, context);
256}
257
258// Vectors of bools are special
259// https://en.wikipedia.org/w/index.php?title=Sequence_container_(C%2B%2B)&oldid=767869909#Specialization_for_bool
260inline void
261dataStore(std::ostream & ostream, std::vector<bool> & v, void * ctx)
262{
263 unsigned int size = v.size();
264 dataStore(ostream, size, nullptr);
265
266 for (const bool val : v)
267 dataStore(ostream, val, ctx);
268}
269
270template <typename T>
271inline void
272dataStore(std::ostream & stream, std::vector<T> & v, void * context)
273{
274 // First store the size of the vector
275 unsigned int size = v.size();
276 dataStore(stream, size, nullptr);
277
278 for (unsigned int i = 0; i < size; i++)
279 dataStore(stream, v[i], context);
280}
281
282template <typename T>
283inline void
284dataStore(std::ostream & stream, std::shared_ptr<T> & v, void * context)
285{
286 T * tmp = v.get();
287
288 dataStore(stream, tmp, context);
289}
290
291template <typename T>
292inline void
293dataStore(std::ostream & stream, std::unique_ptr<T> & v, void * context)
294{
295 T * tmp = v.get();
296
297 dataStore(stream, tmp, context);
298}
299
300template <typename T>
301inline void
302dataStore(std::ostream & stream, std::set<T> & s, void * context)
303{
304 // First store the size of the set
305 unsigned int size = s.size();
306 dataStore(stream, size, nullptr);
307
308 typename std::set<T>::iterator it = s.begin();
309 typename std::set<T>::iterator end = s.end();
310
311 for (; it != end; ++it)
312 {
313 T & x = const_cast<T &>(*it);
314 dataStore(stream, x, context);
315 }
316}
317
318template <typename T>
319inline void
320dataStore(std::ostream & stream, std::list<T> & l, void * context)
321{
322 // First store the size of the set
323 unsigned int size = l.size();
324 dataStore(stream, size, nullptr);
325
326 typename std::list<T>::iterator it = l.begin();
327 typename std::list<T>::iterator end = l.end();
328
329 for (; it != end; ++it)
330 {
331 T & x = const_cast<T &>(*it);
332 dataStore(stream, x, context);
333 }
334}
335
336template <typename T>
337inline void
338dataStore(std::ostream & stream, std::deque<T> & l, void * context)
339{
340 // First store the size of the container
341 unsigned int size = l.size();
342 dataStore(stream, size, nullptr);
343
344 typename std::deque<T>::iterator it = l.begin();
345 typename std::deque<T>::iterator end = l.end();
346
347 for (; it != end; ++it)
348 {
349 T & x = const_cast<T &>(*it);
350 dataStore(stream, x, context);
351 }
352}
353
354template <typename T, typename U>
355inline void
356dataStore(std::ostream & stream, std::map<T, U> & m, void * context)
357{
358 // First store the size of the map
359 unsigned int size = m.size();
360 dataStore(stream, size, nullptr);
361
362 typename std::map<T, U>::iterator it = m.begin();
363 typename std::map<T, U>::iterator end = m.end();
364
365 for (; it != end; ++it)
366 {
367 T & key = const_cast<T &>(it->first);
368
369 dataStore(stream, key, context);
370
371 dataStore(stream, it->second, context);
372 }
373}
374
375template <typename T, typename U>
376inline void
377dataStore(std::ostream & stream, std::unordered_map<T, U> & m, void * context)
378{
379 // First store the size of the map
380 unsigned int size = m.size();
381 dataStore(stream, size, nullptr);
382
383 typename std::unordered_map<T, U>::iterator it = m.begin();
384 typename std::unordered_map<T, U>::iterator end = m.end();
385
386 for (; it != end; ++it)
387 {
388 T & key = const_cast<T &>(it->first);
389
390 dataStore(stream, key, context);
391
392 dataStore(stream, it->second, context);
393 }
394}
395
396template <typename T>
397inline void
398dataStore(std::ostream & stream, std::unordered_set<T> & s, void * context)
399{
400 // First store the size of the set
401 std::size_t size = s.size();
402 dataStore(stream, size, nullptr);
403
404 for (auto & element : s)
405 dataStore(stream, element, context);
406}
407
408template <typename T>
409inline void
410dataStore(std::ostream & stream, std::optional<T> & m, void * context)
411{
412 bool has_value = m.has_value();
413 dataStore(stream, has_value, nullptr);
414
415 if (has_value)
416 dataStore(stream, *m, context);
417}
418
419template <typename T, typename U>
420inline void
421dataStore(std::ostream & stream, HashMap<T, U> & m, void * context)
422{
423 // First store the size of the map
424 unsigned int size = m.size();
425 dataStore(stream, size, nullptr);
426
427 typename HashMap<T, U>::iterator it = m.begin();
428 typename HashMap<T, U>::iterator end = m.end();
429
430 for (; it != end; ++it)
431 {
432 T & key = const_cast<T &>(it->first);
433
434 dataStore(stream, key, context);
435
436 dataStore(stream, it->second, context);
437 }
438}
439
440template <typename T, int Rows, int Cols>
441void
442dataStore(std::ostream & stream, Eigen::Matrix<T, Rows, Cols> & v, void * context)
443{
444 auto m = cast_int<unsigned int>(v.rows());
445 dataStore(stream, m, context);
446 auto n = cast_int<unsigned int>(v.cols());
447 dataStore(stream, n, context);
448 for (const auto i : make_range(m))
449 for (const auto j : make_range(n))
450 {
451 auto & r = v(i, j);
452 dataStore(stream, r, context);
453 }
454}
455
456template <typename T>
457void
458dataStore(std::ostream & stream, GenericTwoVector<T> & v, void * context)
459{
460 dataStore(stream, libMesh::cast_ref<Eigen::Matrix<T, 2, 1> &>(v), context);
461}
462
463// Specializations (defined in .C)
464template <>
465void dataStore(std::ostream & stream, Real & v, void * context);
466template <>
467void dataStore(std::ostream & stream, std::string & v, void * context);
468template <>
469void dataStore(std::ostream & stream, VariableName & v, void * context);
470template <>
471void dataStore(std::ostream & stream, UserObjectName & v, void * context);
472template <>
473void dataStore(std::ostream & stream, bool & v, void * context);
474template <>
475void dataStore(std::ostream & stream, libMesh::FEType & v, void * context);
476template <>
477void dataStore(std::ostream & stream, const Elem *& e, void * context);
478template <>
479void dataStore(std::ostream & stream, const Node *& n, void * context);
480template <>
481void dataStore(std::ostream & stream, Elem *& e, void * context);
482template <>
483void dataStore(std::ostream & stream, Node *& n, void * context);
484template <>
485void dataStore(std::ostream & stream, std::stringstream & s, void * context);
486template <>
487void dataStore(std::ostream & stream, ADReal & dn, void * context);
488#ifdef MOOSE_LIBTORCH_ENABLED
489template <>
490void dataStore(std::ostream & stream, torch::Tensor & t, void * context);
491#endif
492template <>
493void dataStore(std::ostream & stream, libMesh::Parameters & p, void * context);
494#ifdef MOOSE_MFEM_ENABLED
495template <>
496void dataStore(std::ostream & stream, Moose::MFEM::SolutionState & state, void * context);
497#endif
498
509template <>
510void dataStore(std::ostream & stream,
511 std::unique_ptr<libMesh::NumericVector<libMesh::Number>> & v,
512 void * context);
513
514template <typename T, std::size_t N>
515void
516dataStore(std::ostream & stream, T (&arr)[N], void * context)
517{
518 for (std::size_t i = 0; i < N; ++i)
519 dataStore(stream, arr[i], context);
520}
521
522template <typename T, std::size_t N>
523void
524dataStore(std::ostream & stream, std::array<T, N> & arr, void * context)
525{
526 for (std::size_t i = 0; i < N; ++i)
527 dataStore(stream, arr[i], context);
528}
529
530template <typename T>
531void
532dataStore(std::ostream & stream, libMesh::NumericVector<T> & v, void * context)
533{
534 v.close();
535
536 numeric_index_type size = v.local_size();
537
538 for (numeric_index_type i = v.first_local_index(); i < v.first_local_index() + size; i++)
539 {
540 T r = v(i);
541 dataStore(stream, r, context);
542 }
543}
544
545template <>
546void dataStore(std::ostream & stream, Vec & v, void * context);
547
548template <typename T>
549void
550dataStore(std::ostream & stream, DenseVector<T> & v, void * context)
551{
552 unsigned int m = v.size();
553 dataStore(stream, m, nullptr);
554 for (unsigned int i = 0; i < v.size(); i++)
555 {
556 T r = v(i);
557 dataStore(stream, r, context);
558 }
559}
560
561template <typename T>
562void
563dataStore(std::ostream & stream, RankTwoTensorTempl<T> & rtt, void * context)
564{
565 dataStore(stream, rtt._coords, context);
566}
567
568template <typename T>
569void
570dataStore(std::ostream & stream, RankThreeTensorTempl<T> & rtt, void * context)
571{
572 dataStore(stream, rtt._vals, context);
573}
574
575template <typename T>
576void
577dataStore(std::ostream & stream, RankFourTensorTempl<T> & rft, void * context)
578{
579 dataStore(stream, rft._vals, context);
580}
581
582template <typename T>
583void
584dataStore(std::ostream & stream, SymmetricRankTwoTensorTempl<T> & srtt, void * context)
585{
586 dataStore(stream, srtt._vals, context);
587}
588
589template <typename T>
590void
591dataStore(std::ostream & stream, SymmetricRankFourTensorTempl<T> & srft, void * context)
592{
593 dataStore(stream, srft._vals, context);
594}
595
596template <typename T>
597void
598dataStore(std::ostream & stream, ColumnMajorMatrixTempl<T> & cmm, void * context)
599{
600 dataStore(stream, cmm._values, context);
601}
602
609template <typename T>
610void
611dataStore(std::ostream & stream, UniqueStorage<T> & data, void * context)
612{
613 std::size_t size = data.size();
614 dataStore(stream, size, nullptr);
615
616 for (const auto i : index_range(data))
617 {
618 mooseAssert(data.hasValue(i), "Data doesn't have a value");
619 dataStore(stream, data.pointerValue(i), context);
620 }
621}
622
623// DO NOT MODIFY THE NEXT LINE - It is used by MOOSEDocs
624// *************** Global Load Declarations *****************
625template <typename T>
626inline void
627dataLoad(std::istream &, T &, void * /*context*/)
628{
629 static_assert(sizeof(T) == 0, "Must write dataLoad specialization");
630}
631
632namespace moose
633{
634namespace internal
635{
636template <typename T>
637void
638dataLoadSizeof(std::istream & stream, T & v)
639{
640 stream.read((char *)&v, sizeof(v));
641 mooseAssert(!stream.bad(), "Failed to load");
642}
643}
644}
645
646// clang-format off
647#define dataLoadSizeofSpecialization(Type) \
648template <> \
649inline void \
650dataLoad(std::istream & stream, Type & v, void *) \
651{ \
652 moose::internal::dataLoadSizeof(stream, v); \
653}
654// template <>
655// inline void
656// dataLoad(std::istream & stream, const Type & v, void *)
657// {
658// moose::internal::dataLoadSizeof(stream, v);
659// }
660
661dataLoadSizeofSpecialization(bool)
662dataLoadSizeofSpecialization(char)
663dataLoadSizeofSpecialization(signed char)
664dataLoadSizeofSpecialization(short int)
665dataLoadSizeofSpecialization(int)
666dataLoadSizeofSpecialization(long int)
667dataLoadSizeofSpecialization(long long int)
668dataLoadSizeofSpecialization(unsigned char)
669dataLoadSizeofSpecialization(short unsigned int)
670dataLoadSizeofSpecialization(unsigned int)
671dataLoadSizeofSpecialization(long unsigned int)
672dataLoadSizeofSpecialization(long long unsigned int)
673dataLoadSizeofSpecialization(float)
674dataLoadSizeofSpecialization(double)
675dataLoadSizeofSpecialization(long double)
676
677#define dataLoadEnum(EnumType, IntType) \
678template <> \
679inline void dataLoad(std::istream & stream, EnumType & enum_type, void * ctx) \
680{ \
681 IntType loaded; \
682 dataLoad(stream, loaded, ctx); \
683 enum_type = static_cast<EnumType>(loaded); \
684}
685
686dataLoadEnum(libMesh::InfMapType, int)
687dataLoadEnum(libMesh::FEFamily, int)
688dataLoadEnum(libMesh::ParallelType, int)
689dataLoadEnum(Moose::CoordinateSystemType, int)
690dataLoadEnum(libMesh::ElemQuality, int)
691dataLoadEnum(libMesh::ElemType, int)
692dataLoadEnum(libMesh::ElemMappingType, unsigned char)
693
694template <>
695void dataLoad(std::istream &, mt_state &, void *);
696// clang-format on
697
698// Specializations (defined in .C)
699template <>
700void dataLoad(std::istream & stream, std::string & v, void * /*context*/);
701template <>
702void dataLoad(std::istream & stream, VariableName & v, void * /*context*/);
703template <>
704void dataLoad(std::istream & stream, UserObjectName & v, void * /*context*/);
705template <>
706void dataLoad(std::istream & stream, libMesh::FEType & v, void * /*context*/);
707template <>
708void dataLoad(std::istream & stream, const Elem *& e, void * context);
709template <>
710void dataLoad(std::istream & stream, const Node *& e, void * context);
711template <>
712void dataLoad(std::istream & stream, Elem *& e, void * context);
713template <>
714void dataLoad(std::istream & stream, Node *& e, void * context);
715template <>
716void dataLoad(std::istream & stream, std::stringstream & s, void * context);
717template <>
718void dataLoad(std::istream & stream, ADReal & dn, void * context);
719template <>
720void dataLoad(std::istream & stream, libMesh::Parameters & p, void * context);
721template <>
722void dataLoad(std::istream & stream, libMesh::Point & p, void * context);
723
724template <typename T>
725void dataLoad(std::istream & stream, std::shared_ptr<T> & v, void * context);
726
727template <typename T>
728void dataLoad(std::istream & stream, std::unique_ptr<T> & v, void * context);
729
730template <>
748void dataLoad(std::istream & stream,
749 std::unique_ptr<libMesh::NumericVector<libMesh::Number>> & v,
750 void * context);
751
752template <typename T>
753void dataLoad(std::istream & stream, libMesh::TensorValue<T> & v, void * context);
754
755template <typename T>
756void dataLoad(std::istream & stream, libMesh::DenseMatrix<T> & v, void * context);
757
758template <typename T>
759void dataLoad(std::istream & stream, libMesh::DenseVector<T> & v, void * context);
760
761template <typename T>
762void dataLoad(std::istream & stream, libMesh::VectorValue<T> & v, void * context);
763
764template <typename T>
765void dataLoad(std::istream & stream, std::vector<T> & v, void * context);
766
767template <typename T>
768void dataLoad(std::istream & stream, std::set<T> & s, void * context);
769
770template <typename T>
771void dataLoad(std::istream & stream, std::list<T> & l, void * context);
772
773template <typename T>
774void dataLoad(std::istream & stream, std::deque<T> & l, void * context);
775
776template <typename T, typename U>
777void dataLoad(std::istream & stream, std::map<T, U> & m, void * context);
778
779template <typename T, typename U>
780void dataLoad(std::istream & stream, std::unordered_map<T, U> & m, void * context);
781
782template <typename T>
783void dataLoad(std::istream & stream, std::unordered_set<T> & s, void * context);
784
785template <typename T, std::size_t N>
786void dataLoad(std::istream & stream, std::array<T, N> & arr, void * context);
787
788template <typename T, int Rows, int Cols>
789void dataLoad(std::istream & stream, Eigen::Matrix<T, Rows, Cols> & v, void * context);
790
791template <typename T>
792void
793dataLoad(std::istream & /*stream*/, T *& /*v*/, void * /*context*/)
794{
795 mooseError("Attempting to load a raw pointer type: \"",
796 libMesh::demangle(typeid(T).name()),
797 " *\" as restartable data!\nWrite a custom dataLoad() template specialization!\n\n");
798}
799// clang-format on
800
801template <typename T, typename U>
802inline void
803dataLoad(std::istream & stream, std::pair<T, U> & p, void * context)
804{
805 dataLoad(stream, p.first, context);
806 dataLoad(stream, p.second, context);
807}
808
809// Vectors of bools are special
810// https://en.wikipedia.org/w/index.php?title=Sequence_container_(C%2B%2B)&oldid=767869909#Specialization_for_bool
811inline void
812dataLoad(std::istream & stream, std::vector<bool> & v, void * context)
813{
814 // First read the size of the vector
815 unsigned int size = 0;
816 dataLoad(stream, size, nullptr);
817
818 v.resize(size);
819
820 bool tmp{};
821 for (unsigned int i = 0; i < size; i++)
822 {
823 dataLoad(stream, tmp, context);
824 v[i] = tmp;
825 }
826}
827
828template <typename T>
829inline void
830dataLoad(std::istream & stream, std::vector<T> & v, void * context)
831{
832 // First read the size of the vector
833 unsigned int size = 0;
834 dataLoad(stream, size, nullptr);
835
836 v.resize(size);
837
838 for (unsigned int i = 0; i < size; i++)
839 dataLoad(stream, v[i], context);
840}
841
842template <typename T>
843inline void
844dataLoad(std::istream & stream, std::shared_ptr<T> & v, void * context)
845{
846 T * tmp = v.get();
847
848 dataLoad(stream, tmp, context);
849}
850
851template <typename T>
852inline void
853dataLoad(std::istream & stream, std::unique_ptr<T> & v, void * context)
854{
855 T * tmp = v.get();
856
857 dataLoad(stream, tmp, context);
858}
859
860template <typename T>
861inline void
862dataLoad(std::istream & stream, std::set<T> & s, void * context)
863{
864 // First read the size of the set
865 unsigned int size = 0;
866 dataLoad(stream, size, nullptr);
867
868 for (unsigned int i = 0; i < size; i++)
869 {
870 T data;
871 dataLoad(stream, data, context);
872 s.insert(std::move(data));
873 }
874}
875
876template <typename T>
877inline void
878dataLoad(std::istream & stream, std::list<T> & l, void * context)
879{
880 // First read the size of the set
881 unsigned int size = 0;
882 dataLoad(stream, size, nullptr);
883
884 for (unsigned int i = 0; i < size; i++)
885 {
886 T data;
887 dataLoad(stream, data, context);
888 l.push_back(std::move(data));
889 }
890}
891
892template <typename T>
893inline void
894dataLoad(std::istream & stream, std::deque<T> & l, void * context)
895{
896 // First read the size of the container
897 unsigned int size = 0;
898 dataLoad(stream, size, nullptr);
899
900 for (unsigned int i = 0; i < size; i++)
901 {
902 T data;
903 dataLoad(stream, data, context);
904 l.push_back(std::move(data));
905 }
906}
907
908template <typename T, typename U>
909inline void
910dataLoad(std::istream & stream, std::map<T, U> & m, void * context)
911{
912 m.clear();
913
914 // First read the size of the map
915 unsigned int size = 0;
916 dataLoad(stream, size, nullptr);
917
918 for (unsigned int i = 0; i < size; i++)
919 {
920 T key;
921 dataLoad(stream, key, context);
922
923 U & value = m[key];
924 dataLoad(stream, value, context);
925 }
926}
927
928template <typename T, typename U>
929inline void
930dataLoad(std::istream & stream, std::unordered_map<T, U> & m, void * context)
931{
932 m.clear();
933
934 // First read the size of the map
935 unsigned int size = 0;
936 dataLoad(stream, size, nullptr);
937
938 for (unsigned int i = 0; i < size; i++)
939 {
940 T key;
941 dataLoad(stream, key, context);
942
943 U & value = m[key];
944 dataLoad(stream, value, context);
945 }
946}
947
948template <typename T>
949inline void
950dataLoad(std::istream & stream, std::unordered_set<T> & s, void * context)
951{
952 s.clear();
953
954 // First read the size of the set
955 std::size_t size = 0;
956 dataLoad(stream, size, nullptr);
957 s.reserve(size);
958
959 for (std::size_t i = 0; i < size; i++)
960 {
961 T element;
962 dataLoad(stream, element, context);
963 s.insert(element);
964 }
965}
966
967template <typename T>
968inline void
969dataLoad(std::istream & stream, std::optional<T> & m, void * context)
970{
971 bool has_value;
972 dataLoad(stream, has_value, nullptr);
973
974 if (has_value)
975 {
976 m = T{};
977 dataLoad(stream, *m, context);
978 }
979 else
980 m.reset();
981}
982
983template <typename T, typename U>
984inline void
985dataLoad(std::istream & stream, HashMap<T, U> & m, void * context)
986{
987 // First read the size of the map
988 unsigned int size = 0;
989 dataLoad(stream, size, nullptr);
990
991 for (unsigned int i = 0; i < size; i++)
992 {
993 T key;
994 dataLoad(stream, key, context);
995
996 U & value = m[key];
997 dataLoad(stream, value, context);
998 }
999}
1000
1001template <typename T, int Rows, int Cols>
1002void
1003dataLoad(std::istream & stream, Eigen::Matrix<T, Rows, Cols> & v, void * context)
1004{
1005 unsigned int m = 0;
1006 dataLoad(stream, m, context);
1007 unsigned int n = 0;
1008 dataLoad(stream, n, context);
1009 v.resize(m, n);
1010 for (const auto i : make_range(m))
1011 for (const auto j : make_range(n))
1012 {
1013 T r{};
1014 dataLoad(stream, r, context);
1015 v(i, j) = r;
1016 }
1017}
1018
1019template <typename T>
1020void
1021dataLoad(std::istream & stream, GenericTwoVector<T> & v, void * context)
1022{
1023 dataLoad(stream, libMesh::cast_ref<Eigen::Matrix<T, 2, 1> &>(v), context);
1024}
1025
1026// Specializations (defined in .C)
1027template <>
1028void dataLoad(std::istream & stream, Real & v, void * /*context*/);
1029template <>
1030void dataLoad(std::istream & stream, std::string & v, void * /*context*/);
1031template <>
1032void dataLoad(std::istream & stream, VariableName & v, void * /*context*/);
1033template <>
1034void dataLoad(std::istream & stream, UserObjectName & v, void * /*context*/);
1035template <>
1036void dataLoad(std::istream & stream, bool & v, void * /*context*/);
1037template <>
1038void dataLoad(std::istream & stream, libMesh::FEType & v, void * /*context*/);
1039template <>
1040void dataLoad(std::istream & stream, const Elem *& e, void * context);
1041template <>
1042void dataLoad(std::istream & stream, const Node *& e, void * context);
1043template <>
1044void dataLoad(std::istream & stream, Elem *& e, void * context);
1045template <>
1046void dataLoad(std::istream & stream, Node *& e, void * context);
1047template <>
1048void dataLoad(std::istream & stream, std::stringstream & s, void * context);
1049template <>
1050void dataLoad(std::istream & stream, ADReal & dn, void * context);
1051#ifdef MOOSE_LIBTORCH_ENABLED
1052template <>
1053void dataLoad(std::istream & stream, torch::Tensor & t, void * context);
1054#endif
1055template <>
1056void dataLoad(std::istream & stream, libMesh::Parameters & p, void * context);
1057#ifdef MOOSE_MFEM_ENABLED
1058template <>
1059void dataLoad(std::istream & stream, Moose::MFEM::SolutionState & state, void * context);
1060#endif
1078template <>
1079void dataLoad(std::istream & stream,
1080 std::unique_ptr<libMesh::NumericVector<libMesh::Number>> & v,
1081 void * context);
1082
1083template <typename T, std::size_t N>
1084void
1085dataLoad(std::istream & stream, std::array<T, N> & arr, void * context)
1086{
1087 for (std::size_t i = 0; i < N; ++i)
1088 dataLoad(stream, arr[i], context);
1089}
1090
1091template <typename T, std::size_t N>
1092void
1093dataLoad(std::istream & stream, T (&arr)[N], void * context)
1094{
1095 for (std::size_t i = 0; i < N; ++i)
1096 dataLoad(stream, arr[i], context);
1097}
1098
1099template <typename T>
1100void
1101dataLoad(std::istream & stream, libMesh::NumericVector<T> & v, void * context)
1102{
1103 numeric_index_type size = v.local_size();
1104 for (numeric_index_type i = v.first_local_index(); i < v.first_local_index() + size; i++)
1105 {
1106 T r = 0;
1107 dataLoad(stream, r, context);
1108 v.set(i, r);
1109 }
1110 v.close();
1111}
1112
1113template <>
1114void dataLoad(std::istream & stream, Vec & v, void * context);
1115
1116template <typename T>
1117void
1118dataLoad(std::istream & stream, DenseVector<T> & v, void * context)
1119{
1120 unsigned int n = 0;
1121 dataLoad(stream, n, nullptr);
1122 v.resize(n);
1123 for (unsigned int i = 0; i < n; i++)
1124 {
1125 T r = 0;
1126 dataLoad(stream, r, context);
1127 v(i) = r;
1128 }
1129}
1130
1131template <typename T>
1132void
1133dataLoad(std::istream & stream, RankTwoTensorTempl<T> & rtt, void * context)
1134{
1135 dataLoad(stream, rtt._coords, context);
1136}
1137
1138template <typename T>
1139void
1140dataLoad(std::istream & stream, RankThreeTensorTempl<T> & rtt, void * context)
1141{
1142 dataLoad(stream, rtt._vals, context);
1143}
1144
1145template <typename T>
1146void
1147dataLoad(std::istream & stream, RankFourTensorTempl<T> & rft, void * context)
1148{
1149 dataLoad(stream, rft._vals, context);
1150}
1151
1152template <typename T>
1153void
1154dataLoad(std::istream & stream, SymmetricRankTwoTensorTempl<T> & rtt, void * context)
1155{
1156 dataLoad(stream, rtt._vals, context);
1157}
1158
1159template <typename T>
1160void
1161dataLoad(std::istream & stream, SymmetricRankFourTensorTempl<T> & rft, void * context)
1162{
1163 dataLoad(stream, rft._vals, context);
1164}
1165
1166template <typename T>
1167void
1168dataLoad(std::istream & stream, ColumnMajorMatrixTempl<T> & cmm, void * context)
1169{
1170 dataLoad(stream, cmm._values, context);
1171}
1172
1180template <typename T>
1181inline void
1182dataLoad(std::istream & stream, UniqueStorage<T> & data, void * context)
1183{
1184 std::size_t size;
1185 dataLoad(stream, size, nullptr);
1186 data.resize(size);
1187
1188 for (const auto i : index_range(data))
1189 dataLoad(stream, data.pointerValue(i), context);
1190}
1191
1192void dataLoad(std::istream & stream, libMesh::BoundingBox & p, void * context);
1193#ifndef TIMPI_HAVE_STRING_PACKING
1200namespace libMesh
1201{
1202namespace Parallel
1203{
1204template <typename T>
1205class Packing<std::basic_string<T>>
1206{
1207public:
1208 static const unsigned int size_bytes = 4;
1209
1210 typedef T buffer_type;
1211
1212 static unsigned int get_string_len(typename std::vector<T>::const_iterator in)
1213 {
1214 unsigned int string_len = reinterpret_cast<const unsigned char &>(in[size_bytes - 1]);
1215 for (signed int i = size_bytes - 2; i >= 0; --i)
1216 {
1217 string_len *= 256;
1218 string_len += reinterpret_cast<const unsigned char &>(in[i]);
1219 }
1220 return string_len;
1221 }
1222
1223 static unsigned int packed_size(typename std::vector<T>::const_iterator in)
1224 {
1225 return get_string_len(in) + size_bytes;
1226 }
1227
1228 static unsigned int packable_size(const std::basic_string<T> & s, const void *)
1229 {
1230 return s.size() + size_bytes;
1231 }
1232
1233 template <typename Iter>
1234 static void pack(const std::basic_string<T> & b, Iter data_out, const void *)
1235 {
1236 unsigned int string_len = b.size();
1237 for (unsigned int i = 0; i != size_bytes; ++i)
1238 {
1239 *data_out++ = (string_len % 256);
1240 string_len /= 256;
1241 }
1242
1243 std::copy(b.begin(), b.end(), data_out);
1244 }
1245
1246 static std::basic_string<T> unpack(typename std::vector<T>::const_iterator in, void *)
1247 {
1248 unsigned int string_len = get_string_len(in);
1249
1250 std::ostringstream oss;
1251 for (unsigned int i = 0; i < string_len; ++i)
1252 oss << reinterpret_cast<const unsigned char &>(in[i + size_bytes]);
1253
1254 in += size_bytes + string_len;
1255
1256 return oss.str();
1257 }
1258};
1259
1260} // namespace Parallel
1261
1262} // namespace libMesh
1263
1264#endif
DualNumber< Real, DNDerivativeType, true > ADReal
void dataStore(std::ostream &stream, T &v, void *)
Definition DataIO.h:81
void dataLoad(std::istream &, T &, void *)
Definition DataIO.h:627
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
This class defines a Tensor that can change its shape.
A two-component zero initialized vector used for tangential quantities.
Definition TwoVector.h:20
HashMap is an abstraction for dictionary data type, we make it thread-safe by locking inserts.
Definition HashMap.h:19
RankFourTensorTempl is designed to handle any N-dimensional fourth order tensor, C.
T _vals[N4]
The values of the rank-four tensor stored by index=(((i * LIBMESH_DIM + j) * LIBMESH_DIM + k) * LIBME...
RankThreeTensor is designed to handle any N-dimensional third order tensor, r.
T _vals[N3]
The values of the rank-three tensor stored by index=((i * LIBMESH_DIM + j) * LIBMESH_DIM + k)
RankTwoTensorTempl is designed to handle the Stress or Strain Tensor for a fully anisotropic material...
SymmetricRankFourTensorTempl is designed to handle an N-dimensional fourth order tensor with minor sy...
std::array< T, N2 > _vals
The values of the rank-four tensor.
SymmetricRankTwoTensorTempl is designed to handle the Stress or Strain Tensor for an anisotropic mate...
Storage container that stores a vector of unique pointers of T, but represents most of the public fac...
std::size_t size() const
void resize(const std::size_t size)
Resizes the underlying vector.
bool hasValue(const std::size_t i) const
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.
virtual void set(const numeric_index_type i, const T value)=0
virtual void close()=0
virtual numeric_index_type local_size() const=0
virtual numeric_index_type first_local_index() const=0
static unsigned int packed_size(typename std::vector< T >::const_iterator in)
Definition DataIO.h:1223
static void pack(const std::basic_string< T > &b, Iter data_out, const void *)
Definition DataIO.h:1234
static unsigned int get_string_len(typename std::vector< T >::const_iterator in)
Definition DataIO.h:1212
static std::basic_string< T > unpack(typename std::vector< T >::const_iterator in, void *)
Definition DataIO.h:1246
static unsigned int packable_size(const std::basic_string< T > &s, const void *)
Definition DataIO.h:1228
T _coords[LIBMESH_DIM *LIBMESH_DIM]
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).
CoordinateSystemType
Definition MooseTypes.h:864
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
Tnew cast_ref(Told &oldvar)
std::string demangle(const char *name)
void dataStoreSizeof(std::ostream &stream, T &v)
Definition DataIO.h:92
void dataLoadSizeof(std::istream &stream, T &v)
Definition DataIO.h:638