https://mooseframework.inl.gov
BatchMaterial.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 "ElementUserObject.h"
13 
14 #include "libmesh/int_range.h"
15 
16 #include <tuple>
17 #include <string>
18 #include <vector>
19 #include <utility>
20 
22 {
23 
24 // type wrappers
25 template <typename T, unsigned int state>
27 {
28  typedef T type;
30 
31  template <typename M>
32  static gather_type * getPointer(M & mpi, const MaterialPropertyName & name)
33  {
34  return &mpi.template getGenericMaterialProperty<T, false>(name, state);
35  }
36 };
37 
38 template <typename T>
40 template <typename T>
42 
44 {
45  typedef Real type;
46  typedef const VariableValue gather_type;
47 
48  template <typename C>
49  static gather_type * getPointer(const C & coupleable, const VariableName & name)
50  {
51  return &coupleable.coupledValue(name);
52  }
53 };
54 
56 {
57  typedef Real type;
58  typedef const VariableValue gather_type;
59 
60  template <typename C>
61  static gather_type * getPointer(const C & coupleable, const VariableName & name)
62  {
63  return &coupleable.coupledValueOld(name);
64  }
65 };
66 
67 // tuple wrappers
68 struct TupleStd
69 {
70  // the tuple type
71  template <typename... Args>
72  using type = std::tuple<Args...>;
73 
74  // the element access function
75  template <int N, typename T>
76  static auto & get(T && t)
77  {
78  return std::get<N>(t);
79  }
80 
81  // tuple size (number of elements)
82  template <typename T>
83  using size = std::tuple_size<T>;
84 
85  // element type access
86  template <int I, typename T>
87  using element = typename std::tuple_element<I, T>::type;
88 };
89 }
90 
91 template <typename Tuple, typename Output, typename... Input>
93 {
95  typedef BatchMaterial<Tuple, Output, Input...> BatchMaterialType;
96 
98  template <int I, typename T, typename... Names>
99  void construct(T && name, Names &&... names);
100 
102  template <int I>
103  void copy(const unsigned int nqp);
104 
105 public:
107 
108  template <typename... Names>
109  BatchMaterial(const InputParameters & params, Names &&... names)
110  : ElementUserObject(params), _output_ready(false)
111  {
112  construct<0, Names...>(std::forward<Names>(names)...);
113  }
114 
116  virtual void batchCompute() = 0;
117 
121  typedef std::vector<OutputType> OutputVector;
122 
124  typedef typename Tuple::template type<typename Input::type...> InputType;
126  typedef std::vector<InputType> InputVector;
127 
129  const OutputVector & getOutputData() const { return _output_data; }
130 
133 
135  const InputVector & getInputData() const { return _input_data; }
136 
138  bool outputReady() const { return _output_ready; }
139 
141  std::size_t getIndex(dof_id_type elem_id) const;
142 
144  void initialize() override final;
145 
147  void execute() override final;
148 
150  void threadJoin(const UserObject & uo) override final;
151 
153  void finalize() override final;
154 
157 
160 
161  // use regular tuple for the Moose pointers
162  std::tuple<typename Input::gather_type *...> _input_ptr;
163 
165  std::size_t _index;
166 
168  std::map<dof_id_type, std::size_t> _index_map;
169 
170  friend struct BatchMaterialUtils::GatherVariable;
171 
172  friend struct BatchMaterialUtils::GatherVariableOld;
173 
174 protected:
176  virtual bool shouldCompute() { return true; }
177 
178 private:
181 };
182 
183 template <typename Tuple, typename Output, typename... Input>
184 template <int I, typename T, typename... Names>
185 void
187 {
188  // couple the variable or material property
189  typedef typename std::tuple_element<I, std::tuple<Input...>>::type CurrentElement;
190  std::get<I>(_input_ptr) = CurrentElement::getPointer(*this, name);
191 
192  // recursively couple the next variable or material property
193  if constexpr (sizeof...(Names) > 0)
194  construct<I + 1, Names...>(std::forward<Names>(names)...);
195 }
196 
197 template <typename Tuple, typename Output, typename... Input>
198 template <int I>
199 void
201 {
202  // copy all qp values for the current item
203  for (const auto qp : make_range(nqp))
204  Tuple::template get<I>(_input_data[_index + qp]) = (*std::get<I>(_input_ptr))[qp];
205 
206  // proceed to next item
207  if constexpr (I + 1 < sizeof...(Input))
208  copy<I + 1>(nqp);
209 }
210 
211 template <typename Tuple, typename Output, typename... Input>
212 std::size_t
214 {
215  const auto it = _index_map.find(elem_id);
216  if (it == _index_map.end())
217  mooseError("Element ", elem_id, " was not found in the index map for ", name(), ".");
218  return it->second;
219 }
220 
221 template <typename Tuple, typename Output, typename... Input>
222 void
224 {
225  _index = 0;
226 
227  if (shouldCompute())
228  _output_ready = false;
229 }
230 
231 template <typename Tuple, typename Output, typename... Input>
232 void
234 {
235  if (!shouldCompute())
236  return;
237 
238  // update index map
239  _index_map[_current_elem->id()] = _index;
240 
241  // make sure the input data is sized sufficiently big
242  const auto nqp = _qrule->n_points();
243  if (_input_data.size() < _index + nqp)
244  _input_data.resize(_index + nqp);
245 
246  // copy data
247  copy<0>(nqp);
248  _index += nqp;
249 }
250 
251 template <typename Tuple, typename Output, typename... Input>
252 void
254 {
255  if (!shouldCompute())
256  return;
257 
258  // join maps (with index shift)
259  const auto & bm = static_cast<const BatchMaterialType &>(uo);
260  for (const auto & [id, index] : bm._index_map)
261  _index_map[id] = index + _index;
262 
263  // make sure we have enough space
264  const auto capacity = _input_data.capacity();
265  if (capacity < _index + bm._index)
266  _input_data.reserve(_index + bm._index);
267 
268  // concatenate input data
269  _input_data.insert(std::begin(_input_data) + _index,
270  std::begin(bm._input_data),
271  std::begin(bm._input_data) + bm._index);
272  _index += bm._index;
273 }
274 
275 template <typename Tuple, typename Output, typename... Input>
276 void
278 {
279  if (!shouldCompute())
280  return;
281 
282  // resize the input and output data blocks to contain just the gathered items
283  // (should be a no-op mostly)
284  _input_data.resize(_index);
285  _output_data.resize(_index);
286  batchCompute();
287 
288  _output_ready = true;
289 }
std::string name(const ElemQuality q)
void initialize() override final
data structures are initialized here
virtual void batchCompute()=0
override this method to implement the computation on the batch data
static gather_type * getPointer(const C &coupleable, const VariableName &name)
Definition: BatchMaterial.h:61
void threadJoin(const UserObject &uo) override final
Assemble data from all threads.
std::size_t _index
current element index
const MaterialProperty< T > gather_type
Definition: BatchMaterial.h:29
static gather_type * getPointer(M &mpi, const MaterialPropertyName &name)
Definition: BatchMaterial.h:32
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
const InputVector & getInputData() const
get a reference to the input data
static InputParameters validParams()
OutputVector & setOutputData()
Get a writable reference to the output data.
bool shouldCompute(const SubProblem &)
Determine whether the NEML2 material model should be evaluated.
Definition: NEML2Utils.C:59
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
OutputVector _output_data
Output data, written to in batchCompute()
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
void construct(T &&name, Names &&... names)
Helper template for obtainig all variable and material property references.
bool outputReady() const
check if the output is fully computed and ready to be fetched
Based class for output objects.
Definition: Output.h:43
std::tuple< typename Input::gather_type *... > _input_ptr
Output OutputType
The output type is directly specified as a template parameter.
BatchMaterial(const InputParameters &params, Names &&... names)
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:51
Tuple::template type< typename Input::type... > InputType
input data needs to use a flexible tuple type (std::tuple or cuda::std::tuple)
std::map< dof_id_type, std::size_t > _index_map
map from element ID to index
void execute() override final
All data is automatically gathered here.
std::tuple_size< T > size
Definition: BatchMaterial.h:83
std::vector< OutputType > OutputVector
The serialized batch data is stored in a vector.
OutputTools< Real >::VariableValue VariableValue
Definition: MooseTypes.h:314
bool _output_ready
flag that indicates if _output_data has been fully computed
std::size_t getIndex(dof_id_type elem_id) const
get the index for the first qp of a specified element (other qps are at the following indices) ...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
BatchMaterial< Tuple, Output, Input... > BatchMaterialType
shorthand for the instantiated template type
Definition: BatchMaterial.h:95
IntRange< T > make_range(T beg, T end)
const OutputVector & getOutputData() const
Get a read-only reference to the output data.
static InputParameters validParams()
void copy(const unsigned int nqp)
Helper template for gathering all items into the input data tuples.
typename std::tuple_element< I, T >::type element
Definition: BatchMaterial.h:87
static gather_type * getPointer(const C &coupleable, const VariableName &name)
Definition: BatchMaterial.h:49
virtual bool shouldCompute()
Whether we should perform a batch compute.
void finalize() override final
we call batchCompute() from here
InputVector _input_data
Input data, utilized in batchCompute()
Base class for user-specific data.
Definition: UserObject.h:40
std::tuple< Args... > type
Definition: BatchMaterial.h:72
uint8_t dof_id_type
std::vector< InputType > InputVector
The serialized batch data is stored in a vector.