https://mooseframework.inl.gov
MOOSEToNEML2Batched.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 "MOOSEToNEML2.h"
13 #include "ElementUserObject.h"
14 
28 template <typename T>
30 {
31 public:
33 
34  MOOSEToNEML2Batched(const InputParameters & params);
35 
36 #ifndef NEML2_ENABLED
37  void initialize() override {}
38  void execute() override {}
39  void finalize() override {}
40  void threadJoin(const UserObject &) override {}
41 #else
42  void initialize() override;
43  void execute() override;
44  void finalize() override {}
45  void threadJoin(const UserObject &) override;
46 
47  neml2::Tensor gatheredData() const override;
48 
49  // The number of batches
50  std::size_t size() const { return _buffer.size(); }
51 
52 protected:
54  virtual const MooseArray<T> & elemMOOSEData() const = 0;
55 
57  std::vector<T> _buffer;
58 #endif
59 };
60 
61 template <typename T>
64 {
65  auto params = MOOSEToNEML2::validParams();
67 
68  // Since we use the NEML2 model to evaluate the residual AND the Jacobian at the same time, we
69  // want to execute this user object only at execute_on = LINEAR (i.e. during residual evaluation).
70  // The NONLINEAR exec flag below is for computing Jacobian during automatic scaling.
72  execute_options = {EXEC_INITIAL, EXEC_LINEAR, EXEC_NONLINEAR};
73  params.set<ExecFlagEnum>("execute_on") = execute_options;
74 
75  return params;
76 }
77 
78 template <typename T>
80  : MOOSEToNEML2(params), ElementUserObject(params)
81 {
82 }
83 
84 #ifdef NEML2_ENABLED
85 template <typename T>
86 void
88 {
89  _buffer.clear();
90 }
91 
92 template <typename T>
93 void
95 {
96  const auto & elem_data = this->elemMOOSEData();
97  for (auto i : index_range(elem_data))
98  _buffer.push_back(elem_data[i]);
99 }
100 
101 template <typename T>
102 void
104 {
105  // append vectors
106  const auto & m2n = static_cast<const MOOSEToNEML2Batched<T> &>(uo);
107  _buffer.insert(_buffer.end(), m2n._buffer.begin(), m2n._buffer.end());
108 }
109 
110 template <typename T>
111 neml2::Tensor
113 {
114  return NEML2Utils::fromBlob(_buffer);
115 }
116 #endif
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
std::vector< T > _buffer
Intermediate data buffer, filled during the element loop.
static InputParameters validParams()
void execute() override
Execute method.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Generic gatherer for collecting "batched" MOOSE data for NEML2.
neml2::Tensor gatheredData() const override
Convert data gathered from MOOSE into neml2::Tensor.
ExecFlagEnum getDefaultExecFlagEnum()
Return the default ExecFlagEnum for MOOSE.
Definition: MooseUtils.C:1067
virtual const MooseArray< T > & elemMOOSEData() const =0
MOOSE data for the current element.
static InputParameters validParams()
void threadJoin(const UserObject &) override
Must override.
MOOSEToNEML2Batched(const InputParameters &params)
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:29
void initialize() override
Called before execute() is ever called so that data can be cleared.
const ExecFlagType EXEC_NONLINEAR
Definition: Moose.C:31
forward declarations
std::size_t size() const
Common interface for inserting gathered MOOSE data into the NEML2 material model. ...
Definition: MOOSEToNEML2.h:28
static InputParameters validParams()
Definition: MOOSEToNEML2.C:13
auto index_range(const T &sizable)
Base class for user-specific data.
Definition: UserObject.h:40
neml2::Tensor fromBlob(const std::vector< T > &data)
Map from std::vector<T> to neml2::Tensor without copying the data.
Definition: NEML2Utils.h:110
void finalize() override
Finalize.
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:28