libMesh
Loading...
Searching...
No Matches
raw_accessor.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20#ifndef LIBMESH_RAW_ACCESSOR_H
21#define LIBMESH_RAW_ACCESSOR_H
22
23// Local includes
24#include "libmesh/libmesh_common.h"
25
26#include "libmesh/tensor_value.h"
27#include "libmesh/vector_value.h"
28#include "libmesh/type_n_tensor.h"
29
30namespace libMesh
31{
32
36template <typename FieldType>
37struct RawFieldType {};
38
39template <>
41{
42 typedef Number type;
43};
44
45template <>
47{
48 typedef Number type;
49};
50
51template <>
53{
54 typedef Number type;
55};
56
57template<>
59{
60 typedef Number type;
61};
62
63#ifdef LIBMESH_USE_COMPLEX_NUMBERS
64template <>
66{
67 typedef Real type;
68};
69
70template <>
72{
73 typedef Real type;
74};
75
76template <>
78{
79 typedef Real type;
80};
81
82template<>
84{
85 typedef Real type;
86};
87#endif
88
92template <typename FieldType>
94{
95public:
96
97 RawAccessor(FieldType & data, const unsigned int dim)
98 : _data(data),
99 _dim(dim)
100 {}
101
102 ~RawAccessor() = default;
103
104 typename RawFieldType<FieldType>::type & operator()( unsigned int i );
105 const typename RawFieldType<FieldType>::type & operator()( unsigned int i ) const;
106
107private:
109
110 FieldType & _data;
111 const unsigned int _dim;
112};
113
114// Specialize for specific cases
115template<>
116inline
117Number & RawAccessor<Number>::operator()( unsigned int libmesh_dbg_var(i) )
118{
119 libmesh_assert_equal_to (i, 0);
120 return this->_data;
121}
122
123template<>
124inline
126{
127 libmesh_assert_less (i, this->_dim);
128 return this->_data(i);
129}
130
131template<>
132inline
134{
135 libmesh_assert_less (k, this->_dim*this->_dim);
136
137 // For tensors, each row is filled first, i.e. for 2-D
138 // [ 0 1; 2 3]
139 // Thus, k(i,j) = j + i*dim
140 unsigned int ii = k/_dim;
141 unsigned int jj = k - ii*_dim;
142
143 return this->_data(ii,jj);
144}
145
149template <unsigned int N, typename ScalarType>
150class RawAccessor<TypeNTensor<N, ScalarType>>
151{
152public:
153
155
156 RawAccessor(FieldType & data, const unsigned int dim)
157 : _data(data),
158 _dim(dim)
159 {}
160
161 ~RawAccessor() = default;
162
163 typename RawFieldType<FieldType>::type & operator()( unsigned int /*i*/ )
164 { return dummy; }
165
166 const typename RawFieldType<FieldType>::type & operator()( unsigned int /*i*/ ) const
167 { return dummy; }
168
169private:
171
172 ScalarType dummy = 0;
173
175 const unsigned int _dim;
176};
177
178#ifdef LIBMESH_USE_COMPLEX_NUMBERS
179template<>
180inline
181Real & RawAccessor<Real>::operator()( unsigned int libmesh_dbg_var(i) )
182{
183 libmesh_assert_equal_to (i, 0);
184 return this->_data;
185}
186
187template<>
188inline
190{
191 libmesh_assert_less (i, this->_dim);
192 return this->_data(i);
193}
194
195template<>
196inline
198{
199 libmesh_assert_less (k, this->_dim*this->_dim);
200
201 // For tensors, each row is filled first, i.e. for 2-D
202 // [ 0 1; 2 3]
203 // Thus, k(i,j) = i + j*dim
204 unsigned int jj = k/_dim;
205 unsigned int ii = k - jj*_dim;
206
207 return this->_data(ii,jj);
208}
209
210#endif
211
212}
213#endif // LIBMESH_RAW_ACCESSOR_H
unsigned int dim
const RawFieldType< FieldType >::type & operator()(unsigned int) const
RawFieldType< FieldType >::type & operator()(unsigned int)
RawAccessor(FieldType &data, const unsigned int dim)
This class provides single index access to FieldType (i.e.
RawFieldType< FieldType >::type & operator()(unsigned int i)
RawAccessor(FieldType &data, const unsigned int dim)
const RawFieldType< FieldType >::type & operator()(unsigned int i) const
const unsigned int _dim
This class defines a tensor in LIBMESH_DIM dimensional Real or Complex space.
This class will eventually define a rank-N tensor in LIBMESH_DIM dimensional space of type T.
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.
The libMesh namespace provides an interface to certain functionality in the library.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
What underlying data type would we need to access in each field?