https://mooseframework.inl.gov
SerialAccess.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 // MOOSE includes
13 #include "Moose.h"
14 #include "MooseTypes.h"
15 #include "RankTwoTensorForward.h"
16 #include "RankFourTensorForward.h"
17 
18 #include <tuple>
19 
20 namespace Moose
21 {
22 
27 template <typename T>
29 {
30  static_assert(always_false<T>, "Specialize SerialAccess for this type.");
31 };
32 
33 // Specializations for scalar types
34 #define SERIAL_ACCESS_SCALAR(type) \
35  template <> \
36  struct SerialAccess<type> \
37  { \
38  static type * data(type & obj) { return &obj; } \
39  static constexpr std::size_t size(type &) { return 1u; } \
40  static constexpr std::size_t size() { return 1u; } \
41  }
42 
44 SERIAL_ACCESS_SCALAR(const Real);
47 
48 // constant size containers
49 #define SERIAL_ACCESS_CONST_SIZE(type, dataptr, sizeval) \
50  template <typename T> \
51  struct SerialAccess<type<T>> \
52  { \
53  static auto * data(type<T> & obj) { return dataptr; } \
54  static constexpr std::size_t size(type<T> &) { return sizeval; } \
55  static constexpr std::size_t size() { return sizeval; } \
56  }
57 
64  &obj(0u, 0u, 0u, 0u),
66 
67 // dynamic size containers (determining size requires an object instance)
68 #define SERIAL_ACCESS_DYNAMIC_SIZE(type, dataptr, sizeval) \
69  template <typename T> \
70  struct SerialAccess<type<T>> \
71  { \
72  static auto * data(type<T> & obj) { return dataptr; } \
73  static constexpr std::size_t size(type<T> & obj) { return sizeval; } \
74  }
75 
76 SERIAL_ACCESS_DYNAMIC_SIZE(DenseVector, &obj(0u), obj.size());
77 
82 template <typename T>
84 {
85  typedef typename T::value_type value_type;
86 };
87 template <>
89 {
90  typedef ADReal value_type;
91 };
92 template <>
94 {
95  typedef Real value_type;
96 };
97 
98 template <typename T>
100 {
101 public:
105  typedef typename std::conditional<std::is_const_v<T>, const R, R>::type V;
106 
107  class iterator
108  {
109  public:
110  iterator(V * i) : _i(i) {}
111 
112  V & operator*() const { return *_i; }
113 
115  {
116  ++_i;
117  return *this;
118  }
119 
121  {
122  iterator returnval(*this);
123  ++_i;
124  return returnval;
125  }
126 
127  bool operator==(const iterator & j) const { return (_i == j._i); }
128  bool operator!=(const iterator & j) const { return !(*this == j); }
129 
130  private:
131  V * _i;
132  };
133 
135  : _begin(SerialAccess<T>::data(obj)),
136  _end(SerialAccess<T>::data(obj) + SerialAccess<T>::size(obj))
137  {
138  }
139 
140  iterator begin() const { return _begin; }
141  iterator end() const { return _end; }
142 
143  V & operator[](int i) { return *(&*_begin + i); }
144 
145 private:
147 };
148 
149 template <typename T>
151 serialAccess(T & obj)
152 {
153  return SerialAccessRange<T>(obj);
154 }
155 
157 template <typename... Ts>
158 struct TypeList
159 {
160  typedef std::tuple<Ts...> Tuple;
161  typedef std::tuple<Ts *...> PointerTuple;
162  static constexpr std::size_t size = sizeof...(Ts);
163 };
164 
166 template <template <typename, int> class L, int I, typename T, typename... Ts, typename... As>
167 void
169 {
170  L<T, I>::apply(args...);
171  if constexpr (sizeof...(Ts) > 0)
172  typeLoopInternal<L, I + 1>(TypeList<Ts...>{}, args...);
173 }
174 
176 template <template <typename, int> class L, typename... Ts, typename... As>
177 void
179 {
180  typeLoopInternal<L, 0>(TypeList<Ts...>{}, args...);
181 }
182 
183 } // namespace Moose;
RankFourTensorTempl is designed to handle any N-dimensional fourth order tensor, C.
SERIAL_ACCESS_CONST_SIZE(libMesh::VectorValue, &obj(0u), Moose::dim)
void typeLoopInternal(TypeList< T, Ts... >, As... args)
Type loop.
Definition: SerialAccess.h:168
std::conditional< std::is_const_v< T >, const R, R >::type V
Value type with the correct constness.
Definition: SerialAccess.h:105
static constexpr std::size_t size
Definition: SerialAccess.h:162
bool operator!=(const iterator &j) const
Definition: SerialAccess.h:128
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:154
void typeLoop(TypeList< Ts... >, As... args)
Type loop.
Definition: SerialAccess.h:178
Value type helper (necessary for any type that does not have a value_type member or where value_type ...
Definition: SerialAccess.h:83
std::tuple< Ts *... > PointerTuple
Definition: SerialAccess.h:161
bool operator==(const iterator &j) const
Definition: SerialAccess.h:127
DualNumber< Real, DNDerivativeType, true > ADReal
Definition: ADRealForward.h:46
iterator begin() const
Definition: SerialAccess.h:140
std::tuple< Ts... > Tuple
Definition: SerialAccess.h:160
SERIAL_ACCESS_SCALAR(Real)
SerialAccessValueTypeHelper< typename std::remove_const< T >::type >::value_type R
Value type of the components of T.
Definition: SerialAccess.h:103
Serial access requires object data to be stored contiguously.
Definition: SerialAccess.h:28
SerialAccessRange< T > serialAccess(T &obj)
Definition: SerialAccess.h:151
iterator end() const
Definition: SerialAccess.h:141
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
RankTwoTensorTempl is designed to handle the Stress or Strain Tensor for a fully anisotropic material...
Definition: RankTwoTensor.h:87
SERIAL_ACCESS_DYNAMIC_SIZE(DenseVector, &obj(0u), obj.size())
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
Helper structure to hold a list of types.
Definition: SerialAccess.h:158