https://mooseframework.inl.gov
NEML2Assembly.C
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 #ifdef NEML2_ENABLED
11 
12 // Torch includes
13 #include <ATen/ops/from_blob.h>
14 
15 // MOOSE includes
16 #include "NEML2Assembly.h"
17 
18 using namespace libMesh;
19 
21 
24 {
26 
27  params.addClassDescription(
28  "This user object gathers the JxWxT values from all elements in the assembly and "
29  "provides them as a neml2 tensor. This is useful for assembling NEML2 models that "
30  "require the JxWxT values for each element.");
31 
33  execute_options = {EXEC_INITIAL, EXEC_LINEAR};
34  params.set<ExecFlagEnum>("execute_on") = execute_options;
35  params.suppressParameter<ExecFlagEnum>("execute_on");
36 
37  return params;
38 }
39 
41 
42 void
44 {
45  _up_to_date = false;
46 }
47 
48 void
50 {
51  if (_up_to_date)
52  return;
53 
54  _nelem = 0;
55  _nqp = 0;
56  _moose_JxWxT.clear();
57 }
58 
59 void
61 {
62  const auto & other = static_cast<const NEML2Assembly &>(y);
63  mooseAssert(_up_to_date == other._up_to_date,
64  "NEML2Assembly becomes out of sync with other thread");
65 
66  if (_up_to_date)
67  return;
68 
69  _nelem += other._nelem;
70  mooseAssert(_nqp == other._nqp,
71  "The number of quadrature points per element must be the same in all threads.");
72 
73  _moose_JxWxT.insert(_moose_JxWxT.end(), other._moose_JxWxT.begin(), other._moose_JxWxT.end());
74 }
75 
76 void
78 {
79  if (_up_to_date)
80  return;
81 
82  _nelem++;
83 
84  // number of quadrature points
85  if (_nqp != 0 && std::size_t(_nqp) != _q_point.size())
86  mooseError("All elements must have the same number of quadrature points per element for all "
87  "elements");
88  _nqp = _q_point.size();
89 
90  // JxWxT
91  for (auto qp : index_range(_q_point))
92  _moose_JxWxT.push_back(_JxW[qp] * _coord[qp]);
93 }
94 
95 void
97 {
98  TIME_SECTION("finalize", 1, "Updating FEM assembly for NEML2");
99 
100  if (_up_to_date)
101  return;
102 
103  // sanity checks on sizes
104  if (_moose_JxWxT.size() != std::size_t(_nelem * _nqp))
105  mooseError("JxWxT size mismatch, expected ", _nelem * _nqp, " but got ", _moose_JxWxT.size());
106 
107  // convert gathered data to neml2 tensors (and send to device)
108  auto device = _app.getLibtorchDevice();
109  _neml2_JxWxT =
110  neml2::Tensor(at::from_blob(_moose_JxWxT.data(), {_nelem, _nqp}, torch::kFloat64), 2)
111  .to(device);
112 
113  // done
114  _up_to_date = true;
115 }
116 
117 #endif
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
int64_t _nelem
number of elements on this rank
Definition: NEML2Assembly.h:61
const MooseArray< Point > & _q_point
const MooseArray< Real > & _coord
static InputParameters validParams()
bool _up_to_date
Whether the current assembly cache is up to date.
Definition: NEML2Assembly.h:58
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
void finalize() override
Finalize.
Definition: NEML2Assembly.C:96
int64_t _nqp
number of quadrature points per element
Definition: NEML2Assembly.h:63
void initialize() override
Called before execute() is ever called so that data can be cleared.
Definition: NEML2Assembly.C:49
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
void threadJoin(const UserObject &) override
Must override.
Definition: NEML2Assembly.C:60
std::vector< Real > _moose_JxWxT
JxWxT (product of Jacobian determinant, quadrature weight, and coordinate transformation factor) for ...
Definition: NEML2Assembly.h:66
void suppressParameter(const std::string &name)
This method suppresses an inherited parameter so that it isn&#39;t required or valid in the derived class...
ExecFlagEnum getDefaultExecFlagEnum()
Definition: MooseUtils.C:961
static InputParameters validParams()
Definition: NEML2Assembly.C:23
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:259
registerMooseObject("MooseApp", NEML2Assembly)
torch::DeviceType getLibtorchDevice() const
Get the device torch is supposed to be running on.
Definition: MooseApp.h:116
NEML2Assembly(const InputParameters &parameters)
Definition: NEML2Assembly.C:40
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:385
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:31
void execute() override
Execute method.
Definition: NEML2Assembly.C:77
void invalidate()
Invalidate the cached assembly information.
Definition: NEML2Assembly.C:43
This user object caches assembly information from MOOSE.
Definition: NEML2Assembly.h:20
const MooseArray< Real > & _JxW
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:281
neml2::Tensor _neml2_JxWxT
Definition: NEML2Assembly.h:67
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
auto index_range(const T &sizable)
Base class for user-specific data.
Definition: UserObject.h:19
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:30