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"
32 stream.write((
char *)&v,
sizeof(v));
37dataStore(std::ostream & stream, std::string & v,
void * )
40 unsigned int size = v.size();
41 stream.write((
char *)&size,
sizeof(size));
44 stream.write(v.c_str(),
sizeof(
char) * size);
49dataStore(std::ostream & stream, VariableName & v,
void * context)
51 auto & name =
static_cast<std::string &
>(v);
57dataStore(std::ostream & stream, UserObjectName & v,
void * context)
59 auto & name =
static_cast<std::string &
>(v);
67 stream.write((
char *)&v,
sizeof(v));
80#ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
85 dataStore(stream, radial_family, context);
97dataStore(std::ostream & stream, std::vector<bool> & v,
void * context)
130 if (ADReal::do_derivatives)
132 auto & derivatives = dn.derivatives();
133 std::size_t size = derivatives.size();
135 for (MooseIndex(size) i = 0; i < size; ++i)
137 dataStore(stream, derivatives.raw_index(i), context);
138 dataStore(stream, derivatives.raw_at(i), context);
154 mooseError(
"Can't output Elems with invalid ids!");
171 mooseError(
"Can't output Nodes with invalid ids!");
188 mooseError(
"Can't output Elems with invalid ids!");
205 mooseError(
"Can't output Nodes with invalid ids!");
213dataStore(std::ostream & stream, std::stringstream & s,
void * )
215 const std::string & s_str = s.str();
217 size_t s_size = s_str.size();
218 stream.write((
char *)&s_size,
sizeof(s_size));
220 stream.write(s_str.c_str(),
sizeof(
char) * (s_str.size()));
223#ifdef MOOSE_LIBTORCH_ENABLED
226dataStore(std::ostream & stream, torch::Tensor & t,
void * context)
229 mooseAssert(tensor.scalar_type() == at::kDouble,
230 "Restart storage currently supports only double tensors.");
232 auto rank = cast_int<unsigned int>(tensor.dim());
234 for (
unsigned int dim = 0;
dim < rank; ++
dim)
236 auto size = cast_int<unsigned int>(tensor.sizes()[
dim]);
240 const auto flattened = tensor.reshape({tensor.numel()});
241 const auto t_accessor = flattened.accessor<
Real, 1>();
242 for (int64_t i = 0; i < flattened.numel(); ++i)
244 Real r = t_accessor[i];
269 unsigned int m = v.
m();
270 unsigned int n = v.
n();
271 stream.write((
char *)&m,
sizeof(m));
272 stream.write((
char *)&n,
sizeof(n));
273 for (
unsigned int i = 0; i < m; i++)
274 for (
unsigned int j = 0; j < n; j++)
310 storeHelper(stream,
static_cast<std::pair<Point, Point> &
>(bbox), context);
319 stream.write((
char *)&size,
sizeof(size));
324 for (; it != end; ++it)
326 auto & key =
const_cast<std::string &
>(it->first);
327 auto type = it->second->type();
332#define storescalar(ptype) \
333 else if (it->second->type() == demangle(typeid(ptype).name())) storeHelper( \
335 (dynamic_cast<libMesh::Parameters::Parameter<ptype> *>(MooseUtils::get(it->second)))->get(), \
344 storescalar(
unsigned short);
345 storescalar(
unsigned int);
346 storescalar(
unsigned long);
361 bool have_vector = v.get();
366 mooseAssert(context,
"Needs a context of the communicator");
368 mooseAssert(&comm == &v->comm(),
"Inconsistent communicator");
371 mooseError(
"Cannot store ghosted numeric vectors");
374 unsigned int comm_size = comm.size();
382 mooseError(
"Can only store unique_ptrs of PetscVectors");
383 int solver_package_int = solver_package;
384 dataStore(stream, solver_package_int,
nullptr);
402 stream.read((
char *)&v,
sizeof(v));
407dataLoad(std::istream & stream, std::string & v,
void * )
410 unsigned int size = 0;
411 stream.read((
char *)&size,
sizeof(size));
417 stream.read(&v[0],
sizeof(
char) * size);
422dataLoad(std::istream & stream, VariableName & v,
void * context)
424 auto & name =
static_cast<std::string &
>(v);
430dataLoad(std::istream & stream, UserObjectName & v,
void * context)
432 auto & name =
static_cast<std::string &
>(v);
440 stream.read((
char *)&v,
sizeof(v));
453#ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
454 int radial_order = 0;
455 dataLoad(stream, radial_order, context);
467dataLoad(std::istream & stream, std::vector<bool> & v,
void * context)
477 dataLoad(stream, dn.value(), context);
479 if (ADReal::do_derivatives)
481 auto & derivatives = dn.derivatives();
482 std::size_t size = 0;
483 stream.read((
char *)&size,
sizeof(size));
484 derivatives.resize(size);
486 for (MooseIndex(derivatives) i = 0; i < derivatives.size(); ++i)
488 dataLoad(stream, derivatives.raw_index(i), context);
489 dataLoad(stream, derivatives.raw_at(i), context);
499 mooseError(
"Can only load Elem objects using a MooseMesh context!");
509 e =
mesh->elemPtr(
id);
519 mooseError(
"Can only load Node objects using a MooseMesh context!");
529 n =
mesh->nodePtr(
id);
539 mooseError(
"Can only load Elem objects using a MooseMesh context!");
549 e =
mesh->elemPtr(
id);
559 mooseError(
"Can only load Node objects using a MooseMesh context!");
569 n =
mesh->nodePtr(
id);
576dataLoad(std::istream & stream, std::stringstream & s,
void * )
579 stream.read((
char *)&s_size,
sizeof(s_size));
581 std::unique_ptr<char[]> s_s = std::make_unique<char[]>(s_size);
582 stream.read(s_s.get(), s_size);
585 s.str(std::string());
586 s.write(s_s.get(), s_size);
589#ifdef MOOSE_LIBTORCH_ENABLED
592dataLoad(std::istream & stream, torch::Tensor & t,
void * context)
594 unsigned int rank = 0;
597 std::vector<int64_t> sizes(rank);
598 for (
unsigned int dim = 0;
dim < rank; ++
dim)
600 unsigned int size = 0;
605 t = torch::empty(sizes, at::kDouble);
606 auto flattened = t.reshape({t.numel()});
607 auto t_accessor = flattened.accessor<
Real, 1>();
608 for (int64_t i = 0; i < flattened.numel(); ++i)
639 unsigned int m = 0, n = 0;
640 stream.read((
char *)&m,
sizeof(m));
641 stream.read((
char *)&n,
sizeof(n));
643 for (
unsigned int i = 0; i < m; i++)
644 for (
unsigned int j = 0; j < n; j++)
682 loadHelper(stream,
static_cast<std::pair<Point, Point> &
>(bbox), context);
692 unsigned int size = 0;
693 stream.read((
char *)&size,
sizeof(size));
695 for (
unsigned int i = 0; i < size; i++)
697 std::string key, type;
701#define loadscalar(ptype) \
702 else if (type == demangle(typeid(ptype).name())) do \
704 ptype & value = p.set<ptype>(key); \
705 loadHelper(stream, value, context); \
715 loadscalar(
unsigned short);
716 loadscalar(
unsigned int);
717 loadscalar(
unsigned long);
728 dataLoad(stream, have_vector, context);
733 mooseAssert(context,
"Needs a context of the communicator");
736 mooseAssert(&comm == &v->comm(),
"Inconsistent communicator");
739 unsigned int comm_size;
740 dataLoad(stream, comm_size,
nullptr);
741 mooseAssert(comm.size() == comm_size,
"Inconsistent communicator size");
744 int solver_package_int;
745 dataLoad(stream, solver_package_int,
nullptr);
751 dataLoad(stream, local_size,
nullptr);
758 v->init(size, local_size);
761 mooseAssert(v->type() !=
GHOSTED,
"Cannot be ghosted");
765 mooseAssert(v->size() == size,
"Inconsistent size");
766 mooseAssert(v->local_size() == local_size,
"Inconsistent local size");
774dataLoad(std::istream & stream, Vec & v,
void * context)
777 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetLocalSize(v, &local_size));
779 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetArray(v, &array));
780 for (PetscInt i = 0; i < local_size; i++)
781 dataLoad(stream, array[i], context);
783 LibmeshPetscCallA(PETSC_COMM_WORLD, VecRestoreArray(v, &array));
788dataStore(std::ostream & stream, Vec & v,
void * context)
791 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetLocalSize(v, &local_size));
793 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetArray(v, &array));
794 for (PetscInt i = 0; i < local_size; i++)
797 LibmeshPetscCallA(PETSC_COMM_WORLD, VecRestoreArray(v, &array));
DualNumber< Real, DNDerivativeType, true > ADReal
void dataLoad(std::istream &stream, Real &v, void *)
void dataStore(std::ostream &stream, Real &v, void *)
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
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)
void resize(const unsigned int new_m, const unsigned int new_n)
static constexpr dof_id_type invalid_id
OrderWrapper radial_order
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, SolverPackage solver_package=libMesh::default_solver_package(), ParallelType parallel_type=AUTOMATIC)
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.
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)