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" 31 stream.write((
char *)&v,
sizeof(v));
36 dataStore(std::ostream & stream, std::string & v,
void * )
39 unsigned int size = v.size();
40 stream.write((
char *)&size,
sizeof(size));
43 stream.write(v.c_str(),
sizeof(char) * size);
48 dataStore(std::ostream & stream, VariableName & v,
void * context)
50 auto &
name =
static_cast<std::string &
>(v);
56 dataStore(std::ostream & stream, UserObjectName & v,
void * context)
58 auto &
name =
static_cast<std::string &
>(v);
66 stream.write((
char *)&v,
sizeof(v));
79 #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS 84 dataStore(stream, radial_family, context);
96 dataStore(std::ostream & stream, std::vector<bool> & v,
void * context)
129 if (ADReal::do_derivatives)
131 auto & derivatives = dn.derivatives();
132 std::size_t size = derivatives.size();
134 for (MooseIndex(size) i = 0; i < size; ++i)
136 dataStore(stream, derivatives.raw_index(i), context);
137 dataStore(stream, derivatives.raw_at(i), context);
153 mooseError(
"Can't output Elems with invalid ids!");
170 mooseError(
"Can't output Nodes with invalid ids!");
187 mooseError(
"Can't output Elems with invalid ids!");
204 mooseError(
"Can't output Nodes with invalid ids!");
212 dataStore(std::ostream & stream, std::stringstream & s,
void * )
214 const std::string & s_str = s.str();
216 size_t s_size = s_str.size();
217 stream.write((
char *)&s_size,
sizeof(s_size));
219 stream.write(s_str.c_str(),
sizeof(char) * (s_str.size()));
222 #ifdef MOOSE_LIBTORCH_ENABLED 225 dataStore(std::ostream & stream, torch::Tensor & t,
void * context)
228 mooseAssert(tensor.scalar_type() == at::kDouble,
229 "Restart storage currently supports only double tensors.");
231 auto rank = cast_int<unsigned int>(tensor.dim());
233 for (
unsigned int dim = 0;
dim < rank; ++
dim)
235 auto size = cast_int<unsigned int>(tensor.sizes()[
dim]);
239 const auto flattened = tensor.reshape({tensor.numel()});
240 const auto t_accessor = flattened.accessor<
Real, 1>();
241 for (int64_t i = 0; i < flattened.numel(); ++i)
243 Real r = t_accessor[i];
249 template <
typename T>
264 template <
typename T>
268 unsigned int m = v.
m();
269 unsigned int n = v.
n();
270 stream.write((
char *)&m,
sizeof(m));
271 stream.write((
char *)&n,
sizeof(n));
272 for (
unsigned int i = 0; i < m; i++)
273 for (
unsigned int j = 0; j < n; j++)
283 template <
typename T>
315 stream.write((
char *)&size,
sizeof(size));
320 for (; it != end; ++it)
322 auto & key =
const_cast<std::string &
>(it->first);
323 auto type = it->second->type();
328 #define storescalar(ptype) \ 329 else if (it->second->type() == demangle(typeid(ptype).name())) storeHelper( \ 331 (dynamic_cast<libMesh::Parameters::Parameter<ptype> *>(MooseUtils::get(it->second)))->get(), \ 340 storescalar(
unsigned short);
341 storescalar(
unsigned int);
342 storescalar(
unsigned long);
357 bool have_vector = v.get();
362 mooseAssert(context,
"Needs a context of the communicator");
364 mooseAssert(&comm == &v->comm(),
"Inconsistent communicator");
367 mooseError(
"Cannot store ghosted numeric vectors");
370 unsigned int comm_size = comm.size();
378 mooseError(
"Can only store unique_ptrs of PetscVectors");
379 int solver_package_int = solver_package;
380 dataStore(stream, solver_package_int,
nullptr);
398 stream.read((
char *)&v,
sizeof(v));
403 dataLoad(std::istream & stream, std::string & v,
void * )
406 unsigned int size = 0;
407 stream.read((
char *)&size,
sizeof(size));
413 stream.read(&v[0],
sizeof(
char) * size);
418 dataLoad(std::istream & stream, VariableName & v,
void * context)
420 auto &
name =
static_cast<std::string &
>(v);
426 dataLoad(std::istream & stream, UserObjectName & v,
void * context)
428 auto &
name =
static_cast<std::string &
>(v);
436 stream.read((
char *)&v,
sizeof(v));
449 #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS 450 int radial_order = 0;
451 dataLoad(stream, radial_order, context);
463 dataLoad(std::istream & stream, std::vector<bool> & v,
void * context)
473 dataLoad(stream, dn.value(), context);
475 if (ADReal::do_derivatives)
477 auto & derivatives = dn.derivatives();
478 std::size_t size = 0;
479 stream.read((
char *)&size,
sizeof(size));
480 derivatives.resize(size);
482 for (MooseIndex(derivatives) i = 0; i < derivatives.size(); ++i)
484 dataLoad(stream, derivatives.raw_index(i), context);
485 dataLoad(stream, derivatives.raw_at(i), context);
495 mooseError(
"Can only load Elem objects using a MooseMesh context!");
505 e =
mesh->elemPtr(
id);
515 mooseError(
"Can only load Node objects using a MooseMesh context!");
525 n =
mesh->nodePtr(
id);
535 mooseError(
"Can only load Elem objects using a MooseMesh context!");
545 e =
mesh->elemPtr(
id);
555 mooseError(
"Can only load Node objects using a MooseMesh context!");
565 n =
mesh->nodePtr(
id);
572 dataLoad(std::istream & stream, std::stringstream & s,
void * )
575 stream.read((
char *)&s_size,
sizeof(s_size));
577 std::unique_ptr<char[]> s_s = std::make_unique<char[]>(s_size);
578 stream.read(s_s.get(), s_size);
581 s.str(std::string());
582 s.write(s_s.get(), s_size);
585 #ifdef MOOSE_LIBTORCH_ENABLED 588 dataLoad(std::istream & stream, torch::Tensor & t,
void * context)
590 unsigned int rank = 0;
593 std::vector<int64_t> sizes(rank);
594 for (
unsigned int dim = 0;
dim < rank; ++
dim)
596 unsigned int size = 0;
601 t = torch::empty(sizes, at::kDouble);
602 auto flattened = t.reshape({t.numel()});
603 auto t_accessor = flattened.accessor<
Real, 1>();
604 for (int64_t i = 0; i < flattened.numel(); ++i)
613 template <
typename T>
631 template <
typename T>
635 unsigned int m = 0, n = 0;
636 stream.read((
char *)&m,
sizeof(m));
637 stream.read((
char *)&n,
sizeof(n));
639 for (
unsigned int i = 0; i < m; i++)
640 for (
unsigned int j = 0; j < n; j++)
651 template <
typename T>
686 unsigned int size = 0;
687 stream.read((
char *)&size,
sizeof(size));
689 for (
unsigned int i = 0; i < size; i++)
691 std::string key, type;
695 #define loadscalar(ptype) \ 696 else if (type == demangle(typeid(ptype).name())) do \ 698 ptype & value = p.set<ptype>(key); \ 699 loadHelper(stream, value, context); \ 709 loadscalar(
unsigned short);
710 loadscalar(
unsigned int);
711 loadscalar(
unsigned long);
722 dataLoad(stream, have_vector, context);
727 mooseAssert(context,
"Needs a context of the communicator");
730 mooseAssert(&comm == &v->comm(),
"Inconsistent communicator");
733 unsigned int comm_size;
734 dataLoad(stream, comm_size,
nullptr);
735 mooseAssert(comm.size() == comm_size,
"Inconsistent communicator size");
738 int solver_package_int;
739 dataLoad(stream, solver_package_int,
nullptr);
745 dataLoad(stream, local_size,
nullptr);
752 v->init(size, local_size);
755 mooseAssert(v->type() !=
GHOSTED,
"Cannot be ghosted");
759 mooseAssert(v->size() == size,
"Inconsistent size");
760 mooseAssert(v->local_size() == local_size,
"Inconsistent local size");
768 dataLoad(std::istream & stream, Vec & v,
void * context)
771 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetLocalSize(v, &local_size));
773 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetArray(v, &array));
774 for (PetscInt i = 0; i < local_size; i++)
775 dataLoad(stream, array[i], context);
777 LibmeshPetscCallA(PETSC_COMM_WORLD, VecRestoreArray(v, &array));
782 dataStore(std::ostream & stream, Vec & v,
void * context)
785 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetLocalSize(v, &local_size));
787 LibmeshPetscCallA(PETSC_COMM_WORLD, VecGetArray(v, &array));
788 for (PetscInt i = 0; i < local_size; i++)
791 LibmeshPetscCallA(PETSC_COMM_WORLD, VecRestoreArray(v, &array));
std::string name(const ElemQuality q)
RankFourTensorTempl is designed to handle any N-dimensional fourth order tensor, C.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
OrderWrapper radial_order
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...
T _coords[LIBMESH_DIM *LIBMESH_DIM]
DualNumber< Real, DNDerivativeType, true > ADReal
RankThreeTensor is designed to handle any N-dimensional third order tensor, r.
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
static constexpr dof_id_type invalid_id
void dataLoad(std::istream &stream, Real &v, void *)
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
T _vals[N3]
The values of the rank-three tensor stored by index=((i * LIBMESH_DIM + j) * LIBMESH_DIM + k) ...
T _vals[N4]
The values of the rank-four tensor stored by index=(((i * LIBMESH_DIM + j) * LIBMESH_DIM + k) * LIBME...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void resize(const unsigned int new_m, const unsigned int new_n)
IntRange< T > make_range(T beg, T end)
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
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
void dataStore(std::ostream &stream, Real &v, void *)