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