16#include "libmesh/vector_value.h"
17#include "libmesh/tensor_value.h"
18#include "libmesh/fe_type.h"
20#include "libmesh/elem.h"
21#include "libmesh/petsc_vector.h"
22#include "libmesh/enum_solver_package.h"
23#include "libmesh/petsc_solver_exception.h"
24#include "libmesh/bounding_box.h"
39dataStore(std::ostream & stream, std::string & v,
void * )
42 unsigned int size = v.size();
43 stream.write((
char *)&size,
sizeof(size));
46 stream.write(v.c_str(),
sizeof(
char) * size);
51dataStore(std::ostream & stream, VariableName & v,
void * context)
53 auto & name = cast_ref<std::string &>(v);
59dataStore(std::ostream & stream, UserObjectName & v,
void * context)
61 auto & name = cast_ref<std::string &>(v);
67dataStore(std::ostream & stream, FEType & v,
void * context)
69 auto order = v.order.get_order();
72 auto family = v.family;
75#ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
76 auto radial_order = v.radial_order.get_order();
79 auto radial_family = v.radial_family;
80 dataStore(stream, radial_family, context);
82 auto inf_map = v.inf_map;
86 auto p_refinement = v.p_refinement;
92dataStore(std::ostream & stream, std::vector<bool> & v,
void * context)
94 std::size_t size = v.size();
127 if (ADReal::do_derivatives)
129 auto & derivatives = dn.derivatives();
130 std::size_t size = derivatives.size();
132 for (MooseIndex(size) i = 0; i < size; ++i)
134 dataStore(stream, derivatives.raw_index(i), context);
135 dataStore(stream, derivatives.raw_at(i), context);
142dataStore(std::ostream & stream,
const Elem *& e,
void * context)
151 mooseError(
"Can't output Elems with invalid ids!");
159dataStore(std::ostream & stream,
const Node *& n,
void * context)
168 mooseError(
"Can't output Nodes with invalid ids!");
176dataStore(std::ostream & stream, Elem *& e,
void * context)
185 mooseError(
"Can't output Elems with invalid ids!");
193dataStore(std::ostream & stream, Node *& n,
void * context)
202 mooseError(
"Can't output Nodes with invalid ids!");
210dataStore(std::ostream & stream, std::stringstream & s,
void * )
212 const std::string & s_str = s.str();
214 size_t s_size = s_str.size();
215 stream.write((
char *)&s_size,
sizeof(s_size));
217 stream.write(s_str.c_str(),
sizeof(
char) * (s_str.size()));
220#ifdef MOOSE_LIBTORCH_ENABLED
223dataStore(std::ostream & stream, torch::Tensor & t,
void * context)
225 const bool defined = t.defined();
232 mooseAssert(tensor.scalar_type() == at::kDouble,
233 "Restart storage currently supports only double tensors.");
235 auto rank = cast_int<unsigned int>(tensor.dim());
237 for (
unsigned int dim = 0;
dim < rank; ++
dim)
239 auto size = cast_int<unsigned int>(tensor.sizes()[
dim]);
243 const auto flattened = tensor.reshape({tensor.numel()});
244 const auto t_accessor = flattened.accessor<Real, 1>();
245 for (int64_t i = 0; i < flattened.numel(); ++i)
247 Real r = t_accessor[i];
255dataStore(std::ostream & stream, TensorValue<T> & v,
void * context)
265template void dataStore(std::ostream & stream, TensorValue<Real> & v,
void * context);
266template void dataStore(std::ostream & stream, TensorValue<ADReal> & v,
void * context);
270dataStore(std::ostream & stream, DenseMatrix<T> & v,
void * context)
272 unsigned int m = v.m();
273 unsigned int n = v.n();
274 stream.write((
char *)&m,
sizeof(m));
275 stream.write((
char *)&n,
sizeof(n));
276 for (
unsigned int i = 0; i < m; i++)
277 for (
unsigned int j = 0; j < n; j++)
284template void dataStore(std::ostream & stream, DenseMatrix<Real> & v,
void * context);
285template void dataStore(std::ostream & stream, DenseMatrix<ADReal> & v,
void * context);
289dataStore(std::ostream & stream, VectorValue<T> & v,
void * context)
300template void dataStore(std::ostream & stream, VectorValue<Real> & v,
void * context);
301template void dataStore(std::ostream & stream, VectorValue<ADReal> & v,
void * context);
305dataStore(std::ostream & stream,
const Point & p,
void * context)
314 dataStore(stream, cast_ref<std::pair<Point, Point> &>(bbox), context);
319dataStore(std::ostream & stream, Point & p,
void * context)
321 dataStore(stream,
const_cast<const Point &
>(p), context);
330 stream.write((
char *)&size,
sizeof(size));
335 for (; it != end; ++it)
337 auto & key =
const_cast<std::string &
>(it->first);
338 auto type = it->second->type();
343#define storescalar(ptype) \
344 else if (it->second->type() == demangle(typeid(ptype).name())) dataStore( \
346 (dynamic_cast<libMesh::Parameters::Parameter<ptype> *>(MooseUtils::get(it->second)))->get(), \
355 storescalar(
unsigned short);
356 storescalar(
unsigned int);
357 storescalar(
unsigned long);
372 bool have_vector = v.get();
377 mooseAssert(context,
"Needs a context of the communicator");
379 mooseAssert(&comm == &v->comm(),
"Inconsistent communicator");
381 if (v->type() == GHOSTED)
382 mooseError(
"Cannot store ghosted numeric vectors");
385 unsigned int comm_size = comm.size();
393 mooseError(
"Can only store unique_ptrs of PetscVectors");
394 int solver_package_int = solver_package;
395 dataStore(stream, solver_package_int,
nullptr);
398 dof_id_type size = v->size();
400 dof_id_type local_size = v->local_size();
413 dataLoad(stream, foo.statevec, ctx);
414 dataLoad(stream, foo.stateptr, ctx);
415 dataLoad(stream, foo.initialized, ctx);
420dataLoad(std::istream & stream, std::string & v,
void * )
423 unsigned int size = 0;
424 stream.read((
char *)&size,
sizeof(size));
430 stream.read(&v[0],
sizeof(
char) * size);
435dataLoad(std::istream & stream, VariableName & v,
void * context)
437 auto & name = cast_ref<std::string &>(v);
443dataLoad(std::istream & stream, UserObjectName & v,
void * context)
445 auto & name = cast_ref<std::string &>(v);
451dataLoad(std::istream & stream, FEType & v,
void * context)
457 dataLoad(stream, v.family, context);
459#ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
460 int radial_order = 0;
461 dataLoad(stream, radial_order, context);
462 v.radial_order = radial_order;
464 dataLoad(stream, v.radial_family, context);
465 dataLoad(stream, v.inf_map, context);
468 dataLoad(stream, v.p_refinement, context);
473dataLoad(std::istream & stream, std::vector<bool> & v,
void * context)
475 std::size_t size = 0;
478 for (std::size_t i = 0; i < size; i++)
490 dataLoad(stream, dn.value(), context);
492 if (ADReal::do_derivatives)
494 auto & derivatives = dn.derivatives();
495 std::size_t size = 0;
496 stream.read((
char *)&size,
sizeof(size));
497 derivatives.resize(size);
499 for (MooseIndex(derivatives) i = 0; i < derivatives.size(); ++i)
501 dataLoad(stream, derivatives.raw_index(i), context);
502 dataLoad(stream, derivatives.raw_at(i), context);
509dataLoad(std::istream & stream,
const Elem *& e,
void * context)
512 mooseError(
"Can only load Elem objects using a MooseMesh context!");
522 e =
mesh->elemPtr(
id);
529dataLoad(std::istream & stream,
const Node *& n,
void * context)
532 mooseError(
"Can only load Node objects using a MooseMesh context!");
542 n =
mesh->nodePtr(
id);
549dataLoad(std::istream & stream, Elem *& e,
void * context)
552 mooseError(
"Can only load Elem objects using a MooseMesh context!");
562 e =
mesh->elemPtr(
id);
569dataLoad(std::istream & stream, Node *& n,
void * context)
572 mooseError(
"Can only load Node objects using a MooseMesh context!");
582 n =
mesh->nodePtr(
id);
589dataLoad(std::istream & stream, std::stringstream & s,
void * )
592 stream.read((
char *)&s_size,
sizeof(s_size));
594 std::unique_ptr<char[]> s_s = std::make_unique<char[]>(s_size);
595 stream.read(s_s.get(), s_size);
598 s.str(std::string());
599 s.write(s_s.get(), s_size);
602#ifdef MOOSE_LIBTORCH_ENABLED
605dataLoad(std::istream & stream, torch::Tensor & t,
void * context)
607 bool defined =
false;
615 unsigned int rank = 0;
618 std::vector<int64_t> sizes(rank);
619 for (
unsigned int dim = 0;
dim < rank; ++
dim)
621 unsigned int size = 0;
626 t = torch::empty(sizes, at::kDouble);
627 auto flattened = t.reshape({t.numel()});
628 auto t_accessor = flattened.accessor<Real, 1>();
629 for (int64_t i = 0; i < flattened.numel(); ++i)
640dataLoad(std::istream & stream, TensorValue<T> & v,
void * context)
653template void dataLoad(std::istream & stream, TensorValue<Real> & v,
void * context);
654template void dataLoad(std::istream & stream, TensorValue<ADReal> & v,
void * context);
658dataLoad(std::istream & stream, DenseMatrix<T> & v,
void * context)
660 unsigned int m = 0, n = 0;
661 stream.read((
char *)&m,
sizeof(m));
662 stream.read((
char *)&n,
sizeof(n));
664 for (
unsigned int i = 0; i < m; i++)
665 for (
unsigned int j = 0; j < n; j++)
673template void dataLoad(std::istream & stream, DenseMatrix<Real> & v,
void * context);
674template void dataLoad(std::istream & stream, DenseMatrix<ADReal> & v,
void * context);
678dataLoad(std::istream & stream, VectorValue<T> & v,
void * context)
690template void dataLoad(std::istream & stream, VectorValue<Real> & v,
void * context);
691template void dataLoad(std::istream & stream, VectorValue<ADReal> & v,
void * context);
695dataLoad(std::istream & stream, Point & p,
void * context)
704 dataLoad(stream, cast_ref<std::pair<Point, Point> &>(bbox), context);
714 unsigned int size = 0;
715 stream.read((
char *)&size,
sizeof(size));
717 for (
unsigned int i = 0; i < size; i++)
719 std::string key, type;
723#define loadscalar(ptype) \
724 else if (type == demangle(typeid(ptype).name())) do \
726 ptype & value = p.set<ptype>(key); \
727 dataLoad(stream, value, context); \
737 loadscalar(
unsigned short);
738 loadscalar(
unsigned int);
739 loadscalar(
unsigned long);
750 dataLoad(stream, have_vector, context);
755 mooseAssert(context,
"Needs a context of the communicator");
758 mooseAssert(&comm == &v->comm(),
"Inconsistent communicator");
761 unsigned int comm_size;
762 dataLoad(stream, comm_size,
nullptr);
763 mooseAssert(comm.size() == comm_size,
"Inconsistent communicator size");
766 int solver_package_int;
767 dataLoad(stream, solver_package_int,
nullptr);
771 dof_id_type size, local_size;
773 dataLoad(stream, local_size,
nullptr);
779 v = NumericVector<Number>::build(comm, solver_package);
780 v->init(size, local_size);
783 mooseAssert(v->type() != GHOSTED,
"Cannot be ghosted");
787 mooseAssert(v->size() == size,
"Inconsistent size");
788 mooseAssert(v->local_size() == local_size,
"Inconsistent local size");
796dataLoad(std::istream & stream, Vec & v,
void * context)
799 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetLocalSize(v, &local_size));
801 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetArray(v, &array));
802 for (PetscInt i = 0; i < local_size; i++)
803 dataLoad(stream, array[i], context);
805 LibmeshPetscCallA(PETSC_COMM_WORLD, VecRestoreArray(v, &array));
810dataStore(std::ostream & stream, Vec & v,
void * context)
813 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetLocalSize(v, &local_size));
815 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetArray(v, &array));
816 for (PetscInt i = 0; i < local_size; i++)
819 LibmeshPetscCallA(PETSC_COMM_WORLD, VecRestoreArray(v, &array));
DualNumber< Real, DNDerivativeType, true > ADReal
void dataLoad< mt_state >(std::istream &stream, mt_state &foo, void *ctx)
void dataStore< mt_state >(std::ostream &stream, mt_state &foo, void *ctx)
void dataStore(std::ostream &stream, std::string &v, void *)
void dataLoad(std::istream &stream, std::string &v, void *)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
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)
static constexpr dof_id_type invalid_id
std::size_t n_parameters() const
T _coords[LIBMESH_DIM *LIBMESH_DIM]
torch::Tensor toCPUContiguous(const torch::Tensor &tensor)
Return a detached contiguous CPU copy of a tensor.
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.