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
19
22{
24
26 "This user object gathers the JxWxT values from all elements in the assembly and "
27 "provides them as a neml2 tensor. This is useful for assembling NEML2 models that "
28 "require the JxWxT values for each element.");
29
31 execute_options = {EXEC_INITIAL, EXEC_LINEAR};
32 params.set<ExecFlagEnum>("execute_on") = execute_options;
33 params.suppressParameter<ExecFlagEnum>("execute_on");
34
35 return params;
36}
37
39
40void
45
46void
48{
49 if (_up_to_date)
50 return;
51
52 _nelem = 0;
53 _nqp = 0;
54 _moose_JxWxT.clear();
55}
56
57void
59{
60 const auto & other = cast_ref<const NEML2Assembly &>(y);
61 mooseAssert(_up_to_date == other._up_to_date,
62 "NEML2Assembly becomes out of sync with other thread");
63
64 if (_up_to_date)
65 return;
66
67 _nelem += other._nelem;
68 mooseAssert(_nqp == other._nqp,
69 "The number of quadrature points per element must be the same in all threads.");
70
71 _moose_JxWxT.insert(_moose_JxWxT.end(), other._moose_JxWxT.begin(), other._moose_JxWxT.end());
72}
73
74void
76{
77 if (_up_to_date)
78 return;
79
80 _nelem++;
81
82 // number of quadrature points
83 if (_nqp != 0 && std::size_t(_nqp) != _q_point.size())
84 mooseError("All elements must have the same number of quadrature points per element for all "
85 "elements");
86 _nqp = _q_point.size();
87
88 // JxWxT
89 for (auto qp : index_range(_q_point))
90 _moose_JxWxT.push_back(_JxW[qp] * _coord[qp]);
91}
92
93void
95{
96 TIME_SECTION("finalize", 1, "Updating FEM assembly for NEML2");
97
98 if (_up_to_date)
99 return;
100
101 // sanity checks on sizes
102 if (_moose_JxWxT.size() != std::size_t(_nelem * _nqp))
103 mooseError("JxWxT size mismatch, expected ", _nelem * _nqp, " but got ", _moose_JxWxT.size());
104
105 // convert gathered data to neml2 tensors (and send to device)
106 auto device = _app.getLibtorchDevice();
108 neml2::Tensor(at::from_blob(_moose_JxWxT.data(), {_nelem, _nqp}, torch::kFloat64), 2)
109 .to(device);
110
111 // done
112 _up_to_date = true;
113}
114
115#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:31
const ExecFlagType EXEC_LINEAR
Definition Moose.C:32
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