https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
18using namespace libMesh;
19
21
24{
26
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
42void
47
48void
50{
51 if (_up_to_date)
52 return;
53
54 _nelem = 0;
55 _nqp = 0;
56 _moose_JxWxT.clear();
57}
58
59void
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
76void
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
95void
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();
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
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
const ExecFlagType EXEC_INITIAL
Definition Moose.C:30
const ExecFlagType EXEC_LINEAR
Definition Moose.C:31
registerMooseObject("MooseApp", NEML2Assembly)
static InputParameters validParams()
const MooseArray< Real > & _coord
const MooseArray< Real > & _JxW
const MooseArray< Point > & _q_point
A MultiMooseEnum object to hold "execute_on" flags.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void suppressParameter(const std::string &name)
This method suppresses an inherited parameter so that it isn't required or valid in the derived class...
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.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
torch::DeviceType getLibtorchDevice() const
Get the device torch is supposed to be running on.
Definition MooseApp.h:117
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition MooseArray.h:259
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
This user object caches assembly information from MOOSE.
void finalize() override
Finalize.
void execute() override
Execute method.
void initialize() override
Called before execute() is ever called so that data can be cleared.
std::vector< Real > _moose_JxWxT
JxWxT (product of Jacobian determinant, quadrature weight, and coordinate transformation factor) for ...
void invalidate()
Invalidate the cached assembly information.
bool _up_to_date
Whether the current assembly cache is up to date.
static InputParameters validParams()
int64_t _nqp
number of quadrature points per element
int64_t _nelem
number of elements on this rank
void threadJoin(const UserObject &) override
Must override.
neml2::Tensor _neml2_JxWxT
NEML2Assembly(const InputParameters &parameters)
Base class for user-specific data.
Definition UserObject.h:20
ExecFlagEnum getDefaultExecFlagEnum()
Definition MooseUtils.C:972
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
auto index_range(const T &sizable)