https://mooseframework.inl.gov
Loading...
Searching...
No Matches
NEML2Utils.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#include <string>
13
14#ifdef NEML2_ENABLED
15
16#include "neml2/tensors/tensors.h"
17#include "neml2/base/Factory.h"
18#include "neml2/models/Model.h"
19#include "RankTwoTensor.h"
20#include "RankFourTensor.h"
23#include "MaterialProperty.h"
24
25#endif
26
27class MooseObject;
28class Action;
29class SubProblem;
30
31namespace NEML2Utils
32{
33#ifdef NEML2_ENABLED
34
35enum class MOOSEIOType
36{
37 // unbatched
38 TIME,
39 SCALAR,
40 // batched
44};
45
46std::string stringify(MOOSEIOType type);
47
55std::shared_ptr<neml2::Model>
56getModel(neml2::Factory & factory, const std::string & name, neml2::Dtype dtype = neml2::kFloat64);
57
58template <typename T>
59struct Layout
60{
61};
62template <>
63struct Layout<Real>
64{
65 static constexpr std::array<neml2::Size, 0> shape{};
66 static constexpr std::array<neml2::Size, 1> strides{1};
67};
68template <>
69struct Layout<RealVectorValue>
70{
71 static constexpr std::array<neml2::Size, 1> shape{3};
72 static constexpr std::array<neml2::Size, 2> strides{3, 1};
73};
74template <>
76{
77 static constexpr std::array<neml2::Size, 2> shape{3, 3};
78 static constexpr std::array<neml2::Size, 3> strides{9, 3, 1};
79};
80template <>
82{
83 static constexpr std::array<neml2::Size, 1> shape{6};
84 static constexpr std::array<neml2::Size, 2> strides{6, 1};
85};
86template <>
88{
89 static constexpr std::array<neml2::Size, 4> shape{3, 3, 3, 3};
90 static constexpr std::array<neml2::Size, 5> strides{81, 27, 9, 3, 1};
91};
92template <>
94{
95 static constexpr std::array<neml2::Size, 2> shape{6, 6};
96 static constexpr std::array<neml2::Size, 3> strides{36, 6, 1};
97};
98
110template <typename T>
111neml2::Tensor
112fromBlob(const std::vector<T> & data)
113{
114 // The const_cast is fine because torch works with non-const ptr so that it can optionally handle
115 // deallocation. But we are not going to let torch do that.
116 const auto torch_tensor = at::from_blob(const_cast<T *>(data.data()),
117 neml2::utils::add_shapes(data.size(), Layout<T>::shape),
118 at::TensorOptions().dtype(neml2::kFloat64));
119 return neml2::Tensor(torch_tensor, 1);
120}
121
135template <typename T>
136void
137copyTensorToMOOSEData(const at::Tensor & src, T & dest)
138{
139 if (src.dtype() != neml2::kFloat64)
141 "Cannot copy at::Tensor with dtype ", src.dtype(), " into ", demangle(typeid(T).name()));
142 if (src.numel() != Layout<T>::strides[0])
143 mooseError("Cannot copy at::Tensor with shape ",
144 src.sizes(),
145 " into ",
146 demangle(typeid(T).name()),
147 " with different number of elements.");
148 auto dest_tensor = at::from_blob(reinterpret_cast<Real *>(&dest),
150 at::TensorOptions().dtype(neml2::kFloat64));
151 dest_tensor.copy_(src.reshape(Layout<T>::shape));
152}
153
154static std::string NEML2_help_message = R""""(
155==============================================================================
156To debug NEML2 related issues:
1571. Build and run MOOSE in dbg mode.
1582. Re-run the simulation using the dbg executable, and often times
159 NEML2 will provide a more helpful error message.
1603. If the error message is not helpful, or if there is still no error message,
161 run the simulation through a debugger: See
162 https://mooseframework.inl.gov/application_development/debugging.html
1634. If the issue is due to a NEML2 bug, feel free to report it at
164 https://github.com/applied-material-modeling/neml2/issues
165==============================================================================
166)"""";
167
168#endif // NEML2_ENABLED
169
171bool shouldCompute(const SubProblem &);
172
176std::string docstring(const std::string & desc);
177
181void assertNEML2Enabled();
182
183} // namespace NEML2Utils
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Base class for actions.
Definition Action.h:38
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
RankFourTensorTempl is designed to handle any N-dimensional fourth order tensor, C.
Generic class for solving transient nonlinear problems.
Definition SubProblem.h:79
SymmetricRankFourTensorTempl is designed to handle an N-dimensional fourth order tensor with minor sy...
neml2::Tensor fromBlob(const std::vector< T > &data)
Map from std::vector<T> to neml2::Tensor without copying the data.
Definition NEML2Utils.h:112
std::shared_ptr< neml2::Model > getModel(neml2::Factory &factory, const std::string &name, neml2::Dtype dtype=neml2::kFloat64)
Get the NEML2 Model.
Definition NEML2Utils.C:38
std::string docstring(const std::string &desc)
Augment docstring if NEML2 is not enabled.
Definition NEML2Utils.C:71
bool shouldCompute(const SubProblem &)
Determine whether the NEML2 material model should be evaluated.
Definition NEML2Utils.C:53
void copyTensorToMOOSEData(const at::Tensor &src, T &dest)
Directly copy a contiguous chunk of memory of a at::Tensor to a MOOSE data of type T.
Definition NEML2Utils.h:137
void assertNEML2Enabled()
Assert that NEML2 is enabled.
Definition NEML2Utils.C:81
std::string stringify(MOOSEIOType type)
Definition NEML2Utils.C:18
static std::string NEML2_help_message
Definition NEML2Utils.h:154