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;
117 if (ADReal::do_derivatives)
119 auto & derivatives = dn.derivatives();
120 std::size_t size = derivatives.size();
122 for (MooseIndex(size) i = 0; i < size; ++i)
124 dataStore(stream, derivatives.raw_index(i), context);
125 dataStore(stream, derivatives.raw_at(i), context);
132dataStore(std::ostream & stream,
const Elem *& e,
void * context)
141 mooseError(
"Can't output Elems with invalid ids!");
149dataStore(std::ostream & stream,
const Node *& n,
void * context)
158 mooseError(
"Can't output Nodes with invalid ids!");
166dataStore(std::ostream & stream, Elem *& e,
void * context)
175 mooseError(
"Can't output Elems with invalid ids!");
183dataStore(std::ostream & stream, Node *& n,
void * context)
192 mooseError(
"Can't output Nodes with invalid ids!");
200dataStore(std::ostream & stream, std::stringstream & s,
void * )
202 const std::string & s_str = s.str();
204 size_t s_size = s_str.size();
205 stream.write((
char *)&s_size,
sizeof(s_size));
207 stream.write(s_str.c_str(),
sizeof(
char) * (s_str.size()));
210#ifdef MOOSE_LIBTORCH_ENABLED
213dataStore(std::ostream & stream, torch::Tensor & t,
void * context)
216 mooseAssert(tensor.scalar_type() == at::kDouble,
217 "Restart storage currently supports only double tensors.");
219 auto rank = cast_int<unsigned int>(tensor.dim());
221 for (
unsigned int dim = 0;
dim < rank; ++
dim)
223 auto size = cast_int<unsigned int>(tensor.sizes()[
dim]);
227 const auto flattened = tensor.reshape({tensor.numel()});
228 const auto t_accessor = flattened.accessor<Real, 1>();
229 for (int64_t i = 0; i < flattened.numel(); ++i)
231 Real r = t_accessor[i];
239dataStore(std::ostream & stream, TensorValue<T> & v,
void * context)
249template void dataStore(std::ostream & stream, TensorValue<Real> & v,
void * context);
250template void dataStore(std::ostream & stream, TensorValue<ADReal> & v,
void * context);
254dataStore(std::ostream & stream, DenseMatrix<T> & v,
void * context)
256 unsigned int m = v.m();
257 unsigned int n = v.n();
258 stream.write((
char *)&m,
sizeof(m));
259 stream.write((
char *)&n,
sizeof(n));
260 for (
unsigned int i = 0; i < m; i++)
261 for (
unsigned int j = 0; j < n; j++)
268template void dataStore(std::ostream & stream, DenseMatrix<Real> & v,
void * context);
269template void dataStore(std::ostream & stream, DenseMatrix<ADReal> & v,
void * context);
273dataStore(std::ostream & stream, VectorValue<T> & v,
void * context)
284template void dataStore(std::ostream & stream, VectorValue<Real> & v,
void * context);
285template void dataStore(std::ostream & stream, VectorValue<ADReal> & v,
void * context);
289dataStore(std::ostream & stream,
const Point & p,
void * context)
298 dataStore(stream, cast_ref<std::pair<Point, Point> &>(bbox), context);
303dataStore(std::ostream & stream, Point & p,
void * context)
305 dataStore(stream,
const_cast<const Point &
>(p), context);
314 stream.write((
char *)&size,
sizeof(size));
319 for (; it != end; ++it)
321 auto & key =
const_cast<std::string &
>(it->first);
322 auto type = it->second->type();
327#define storescalar(ptype) \
328 else if (it->second->type() == demangle(typeid(ptype).name())) dataStore( \
330 (dynamic_cast<libMesh::Parameters::Parameter<ptype> *>(MooseUtils::get(it->second)))->get(), \
339 storescalar(
unsigned short);
340 storescalar(
unsigned int);
341 storescalar(
unsigned long);
356 bool have_vector = v.get();
361 mooseAssert(context,
"Needs a context of the communicator");
363 mooseAssert(&comm == &v->comm(),
"Inconsistent communicator");
365 if (v->type() == GHOSTED)
366 mooseError(
"Cannot store ghosted numeric vectors");
369 unsigned int comm_size = comm.size();
377 mooseError(
"Can only store unique_ptrs of PetscVectors");
378 int solver_package_int = solver_package;
379 dataStore(stream, solver_package_int,
nullptr);
382 dof_id_type size = v->size();
384 dof_id_type local_size = v->local_size();
397 dataLoad(stream, foo.statevec, ctx);
398 dataLoad(stream, foo.stateptr, ctx);
399 dataLoad(stream, foo.initialized, ctx);
404dataLoad(std::istream & stream, std::string & v,
void * )
407 unsigned int size = 0;
408 stream.read((
char *)&size,
sizeof(size));
414 stream.read(&v[0],
sizeof(
char) * size);
419dataLoad(std::istream & stream, VariableName & v,
void * context)
421 auto & name = cast_ref<std::string &>(v);
427dataLoad(std::istream & stream, UserObjectName & v,
void * context)
429 auto & name = cast_ref<std::string &>(v);
435dataLoad(std::istream & stream, FEType & v,
void * context)
441 dataLoad(stream, v.family, context);
443#ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
444 int radial_order = 0;
445 dataLoad(stream, radial_order, context);
446 v.radial_order = radial_order;
448 dataLoad(stream, v.radial_family, context);
449 dataLoad(stream, v.inf_map, context);
452 dataLoad(stream, v.p_refinement, context);
459 dataLoad(stream, dn.value(), context);
461 if (ADReal::do_derivatives)
463 auto & derivatives = dn.derivatives();
464 std::size_t size = 0;
465 stream.read((
char *)&size,
sizeof(size));
466 derivatives.resize(size);
468 for (MooseIndex(derivatives) i = 0; i < derivatives.size(); ++i)
470 dataLoad(stream, derivatives.raw_index(i), context);
471 dataLoad(stream, derivatives.raw_at(i), context);
478dataLoad(std::istream & stream,
const Elem *& e,
void * context)
481 mooseError(
"Can only load Elem objects using a MooseMesh context!");
491 e =
mesh->elemPtr(
id);
498dataLoad(std::istream & stream,
const Node *& n,
void * context)
501 mooseError(
"Can only load Node objects using a MooseMesh context!");
511 n =
mesh->nodePtr(
id);
518dataLoad(std::istream & stream, Elem *& e,
void * context)
521 mooseError(
"Can only load Elem objects using a MooseMesh context!");
531 e =
mesh->elemPtr(
id);
538dataLoad(std::istream & stream, Node *& n,
void * context)
541 mooseError(
"Can only load Node objects using a MooseMesh context!");
551 n =
mesh->nodePtr(
id);
558dataLoad(std::istream & stream, std::stringstream & s,
void * )
561 stream.read((
char *)&s_size,
sizeof(s_size));
563 std::unique_ptr<char[]> s_s = std::make_unique<char[]>(s_size);
564 stream.read(s_s.get(), s_size);
567 s.str(std::string());
568 s.write(s_s.get(), s_size);
571#ifdef MOOSE_LIBTORCH_ENABLED
574dataLoad(std::istream & stream, torch::Tensor & t,
void * context)
576 unsigned int rank = 0;
579 std::vector<int64_t> sizes(rank);
580 for (
unsigned int dim = 0;
dim < rank; ++
dim)
582 unsigned int size = 0;
587 t = torch::empty(sizes, at::kDouble);
588 auto flattened = t.reshape({t.numel()});
589 auto t_accessor = flattened.accessor<Real, 1>();
590 for (int64_t i = 0; i < flattened.numel(); ++i)
601dataLoad(std::istream & stream, TensorValue<T> & v,
void * context)
614template void dataLoad(std::istream & stream, TensorValue<Real> & v,
void * context);
615template void dataLoad(std::istream & stream, TensorValue<ADReal> & v,
void * context);
619dataLoad(std::istream & stream, DenseMatrix<T> & v,
void * context)
621 unsigned int m = 0, n = 0;
622 stream.read((
char *)&m,
sizeof(m));
623 stream.read((
char *)&n,
sizeof(n));
625 for (
unsigned int i = 0; i < m; i++)
626 for (
unsigned int j = 0; j < n; j++)
634template void dataLoad(std::istream & stream, DenseMatrix<Real> & v,
void * context);
635template void dataLoad(std::istream & stream, DenseMatrix<ADReal> & v,
void * context);
639dataLoad(std::istream & stream, VectorValue<T> & v,
void * context)
651template void dataLoad(std::istream & stream, VectorValue<Real> & v,
void * context);
652template void dataLoad(std::istream & stream, VectorValue<ADReal> & v,
void * context);
656dataLoad(std::istream & stream, Point & p,
void * context)
665 dataLoad(stream, cast_ref<std::pair<Point, Point> &>(bbox), context);
675 unsigned int size = 0;
676 stream.read((
char *)&size,
sizeof(size));
678 for (
unsigned int i = 0; i < size; i++)
680 std::string key, type;
684#define loadscalar(ptype) \
685 else if (type == demangle(typeid(ptype).name())) do \
687 ptype & value = p.set<ptype>(key); \
688 dataLoad(stream, value, context); \
698 loadscalar(
unsigned short);
699 loadscalar(
unsigned int);
700 loadscalar(
unsigned long);
711 dataLoad(stream, have_vector, context);
716 mooseAssert(context,
"Needs a context of the communicator");
719 mooseAssert(&comm == &v->comm(),
"Inconsistent communicator");
722 unsigned int comm_size;
723 dataLoad(stream, comm_size,
nullptr);
724 mooseAssert(comm.size() == comm_size,
"Inconsistent communicator size");
727 int solver_package_int;
728 dataLoad(stream, solver_package_int,
nullptr);
732 dof_id_type size, local_size;
734 dataLoad(stream, local_size,
nullptr);
740 v = NumericVector<Number>::build(comm, solver_package);
741 v->init(size, local_size);
744 mooseAssert(v->type() != GHOSTED,
"Cannot be ghosted");
748 mooseAssert(v->size() == size,
"Inconsistent size");
749 mooseAssert(v->local_size() == local_size,
"Inconsistent local size");
757dataLoad(std::istream & stream, Vec & v,
void * context)
760 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetLocalSize(v, &local_size));
762 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetArray(v, &array));
763 for (PetscInt i = 0; i < local_size; i++)
764 dataLoad(stream, array[i], context);
766 LibmeshPetscCallA(PETSC_COMM_WORLD, VecRestoreArray(v, &array));
771dataStore(std::ostream & stream, Vec & v,
void * context)
774 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetLocalSize(v, &local_size));
776 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetArray(v, &array));
777 for (PetscInt i = 0; i < local_size; i++)
780 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.