fixed_size_vector.h
Go to the documentation of this file.
1// LIC// ====================================================================
2// LIC// This file forms part of oomph-lib, the object-oriented,
3// LIC// multi-physics finite-element library, available
4// LIC// at http://www.oomph-lib.org.
5// LIC//
6// LIC// Copyright (C) 2006-2026 Matthias Heil and Andrew Hazel
7// LIC//
8// LIC// This library is free software; you can redistribute it and/or
9// LIC// modify it under the terms of the GNU Lesser General Public
10// LIC// License as published by the Free Software Foundation; either
11// LIC// version 2.1 of the License, or (at your option) any later version.
12// LIC//
13// LIC// This library is distributed in the hope that it will be useful,
14// LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// LIC// Lesser General Public License for more details.
17// LIC//
18// LIC// You should have received a copy of the GNU Lesser General Public
19// LIC// License along with this library; if not, write to the Free Software
20// LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21// LIC// 02110-1301 USA.
22// LIC//
23// LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
24// LIC//
25// LIC//====================================================================
26
27#ifndef OOMPH_FIXED_SIZE_VECTOR_HEADER
28#define OOMPH_FIXED_SIZE_VECTOR_HEADER
29
30
31// Standard library includes
32#include <initializer_list>
33#include <sstream>
34
35#include "oomph_definitions.h"
36
37namespace oomph
38{
39
40
41 //============================================================
42 /// Class for fixed-size vector storing N objects of type T.
43 /// No resize or other STL functionality but (therefore)
44 /// much less run-time overhead.
45 //============================================================
46 template<class T, unsigned N>
48 {
49 public:
50 /// Constructor; no initialisiation
52
53 /// Constructor with initialisation to value
54 FixedSizeVector(const T& value)
55 {
56 this->fill(value);
57 }
58
59
60 /// Constructor from initializer list. Allows
61 /// FixedSizeVector<double,3> = {0.0,1.0,2.0};
62 /// say.
63 FixedSizeVector(std::initializer_list<T> values)
64 {
65#ifdef RANGE_CHECKING
66 if (values.size() != N)
67 {
68 std::stringstream error_message;
69
70 error_message << "Initialiser list contains " << values.size()
71 << " entries but FixedSizeVector has size " << N
72 << std::endl;
73
74 throw OomphLibError(error_message.str(),
77 }
78#endif
79
80 unsigned i = 0;
81 for (const auto& value : values)
82 {
83 Data[i++] = value;
84 }
85 }
86
87
88 /// Fill all entries with specified value
89 void fill(const T& value)
90 {
91 for (unsigned i = 0; i < N; i++)
92 {
93 Data[i] = value;
94 }
95 }
96
97 /// Size of vector
98 unsigned size() const
99 {
100 return N;
101 }
102
103 /// Read/write access to i-th entry
104 T& operator[](const unsigned& i)
105 {
106#ifdef RANGE_CHECKING
107 if (i >= N)
108 {
109 std::stringstream error_message;
110 error_message << "Range error: trying to access entry " << i
111 << " in a fixed-size vector of size " << N << std::endl;
112 throw OomphLibError(error_message.str(),
115 }
116#endif
117
118 return Data[i];
119 }
120
121
122 /// Read access to i-th entry
123 const T& operator[](const unsigned& i) const
124 {
125#ifdef RANGE_CHECKING
126 if (i >= N)
127 {
128 std::stringstream error_message;
129 error_message << "Range error: trying to access entry " << i
130 << " in a fixed-size vector of size " << N << std::endl;
131 throw OomphLibError(error_message.str(),
134 }
135#endif
136
137 return Data[i];
138 }
139
140 private:
141 /// The data, stored as a raw C-style array
142 T Data[N];
143 };
144
145
146 ////////////////////////////////////////////////////////////
147 ////////////////////////////////////////////////////////////
148 ////////////////////////////////////////////////////////////
149
150
151 //============================================================
152 /// Class for fixed-size matrix storing NROW x NCOL objects
153 /// of type T.
154 ///
155 /// No resize or other STL functionality but therefore
156 /// much less run-time overhead.
157 ///
158 /// Storage is row-major:
159 ///
160 /// (0,0), (0,1), ..., (0,NCOL-1),
161 /// (1,0), (1,1), ..., (1,NCOL-1),
162 /// ...
163 /// (NROW-1,NCOL-1)
164 //============================================================
165 template<class T, unsigned NROW, unsigned NCOL>
167 {
168 public:
169 /// Default constructor
171
172 /// Construct and fill all entries with specified value
173 FixedSizeMatrix(const T& value)
174 {
175 this->fill(value);
176 }
177
178 // hierher copy into vector.
179
180 /// Fill all entries with specified value
181 void fill(const T& value)
182 {
183 for (unsigned i = 0; i < NROW * NCOL; i++)
184 {
185 Data[i] = value;
186 }
187 }
188
189 /// Number of rows
190 unsigned nrow() const
191 {
192 return NROW;
193 }
194
195 /// Number of columns
196 unsigned ncol() const
197 {
198 return NCOL;
199 }
200
201 /// Read/write access to (i,j)-th entry
202 T& operator()(const unsigned& i, const unsigned& j)
203 {
204#ifdef RANGE_CHECKING
205 if ((i >= NROW) || (j >= NCOL))
206 {
207 std::stringstream error_message;
208
209 error_message << "Range error: trying to access entry (" << i << ","
210 << j << ") "
211 << "in a fixed-size matrix of size " << NROW << " x "
212 << NCOL << std::endl;
213
214 throw OomphLibError(error_message.str(),
217 }
218#endif
219
220 return Data[i * NCOL + j];
221 }
222
223 /// Read access to (i,j)-th entry
224 const T& operator()(const unsigned& i, const unsigned& j) const
225 {
226#ifdef RANGE_CHECKING
227 if ((i >= NROW) || (j >= NCOL))
228 {
229 std::stringstream error_message;
230
231 error_message << "Range error: trying to access entry (" << i << ","
232 << j << ") "
233 << "in a fixed-size matrix of size " << NROW << " x "
234 << NCOL << std::endl;
235
236 throw OomphLibError(error_message.str(),
239 }
240#endif
241
242 return Data[i * NCOL + j];
243 }
244
245 /// Fill all entries with specified value
246 void initialise(const T& value)
247 {
248 for (unsigned i = 0; i < NROW * NCOL; i++)
249 {
250 Data[i] = value;
251 }
252 }
253
254 private:
255 /// Matrix entries stored in row-major order
257 };
258
259
260 ////////////////////////////////////////////////////////////
261 ////////////////////////////////////////////////////////////
262 ////////////////////////////////////////////////////////////
263
264
265 //============================================================
266 /// Class for fixed-size rank-three tensor storing
267 /// N1 x N2 x N3 objects of type T.
268 ///
269 /// Storage layout:
270 ///
271 /// (0,0,0), (0,0,1), ... , (0,0,N3-1),
272 /// (0,1,0), ...
273 /// ...
274 /// (N1-1,N2-1,N3-1)
275 //============================================================
276 template<class T, unsigned N1, unsigned N2, unsigned N3>
278 {
279 public:
280 /// Default constructor
282
283 /// Constructor with initialisation
285 {
286 this->fill(value);
287 }
288
289 /// Fill all entries with specified value
290 void fill(const T& value)
291 {
292 for (unsigned i = 0; i < N1 * N2 * N3; i++)
293 {
294 Data[i] = value;
295 }
296 }
297
298 /// Read/write access to (i,j,k)-th entry
299 T& operator()(const unsigned& i, const unsigned& j, const unsigned& k)
300 {
301#ifdef RANGE_CHECKING
302 if ((i >= N1) || (j >= N2) || (k >= N3))
303 {
304 std::stringstream error_message;
305
306 error_message << "Range error: trying to access entry (" << i << ","
307 << j << "," << k << ") "
308 << "in a fixed-size rank-three tensor of size " << N1
309 << " x " << N2 << " x " << N3 << std::endl;
310
311 throw OomphLibError(error_message.str(),
314 }
315#endif
316
317 return Data[(i * N2 + j) * N3 + k];
318 }
319
320 /// Read access to (i,j,k)-th entry
321 const T& operator()(const unsigned& i,
322 const unsigned& j,
323 const unsigned& k) const
324 {
325#ifdef RANGE_CHECKING
326 if ((i >= N1) || (j >= N2) || (k >= N3))
327 {
328 std::stringstream error_message;
329
330 error_message << "Range error: trying to access entry (" << i << ","
331 << j << "," << k << ") "
332 << "in a fixed-size rank-three tensor of size " << N1
333 << " x " << N2 << " x " << N3 << std::endl;
334
335 throw OomphLibError(error_message.str(),
338 }
339#endif
340
341 return Data[(i * N2 + j) * N3 + k];
342 }
343
344 private:
345 /// Tensor data
346 T Data[N1 * N2 * N3];
347 };
348
349
350 ////////////////////////////////////////////////////////////
351 ////////////////////////////////////////////////////////////
352 ////////////////////////////////////////////////////////////
353
354 //============================================================
355 /// Class for fixed-size rank-four tensor storing
356 /// N1 x N2 x N3 x N4 objects of type T.
357 ///
358 /// Storage layout:
359 ///
360 /// (0,0,0,0), (0,0,0,1), ... , (0,0,0,N4-1),
361 /// (0,0,1,0), ...
362 /// ...
363 /// (N1-1,N2-1,N3-1,N4-1)
364 //============================================================
365 template<class T, unsigned N1, unsigned N2, unsigned N3, unsigned N4>
367 {
368 public:
369 /// Default constructor
371
372 /// Constructor with initialisation
374 {
375 this->fill(value);
376 }
377
378 /// Fill all entries with specified value
379 void fill(const T& value)
380 {
381 for (unsigned i = 0; i < N1 * N2 * N3 * N4; i++)
382 {
383 Data[i] = value;
384 }
385 }
386
387 /// Read/write access to (i,j,k,l)-th entry
388 T& operator()(const unsigned& i,
389 const unsigned& j,
390 const unsigned& k,
391 const unsigned& l)
392 {
393#ifdef RANGE_CHECKING
394 if ((i >= N1) || (j >= N2) || (k >= N3) || (l >= N4))
395 {
396 std::stringstream error_message;
397
398 error_message << "Range error: trying to access entry (" << i << ","
399 << j << "," << k << "," << l << ") "
400 << "in a fixed-size rank-four tensor of size " << N1
401 << " x " << N2 << " x " << N3 << " x " << N4 << std::endl;
402
403 throw OomphLibError(error_message.str(),
406 }
407#endif
408
409 return Data[((i * N2 + j) * N3 + k) * N4 + l];
410 }
411
412 /// Read access to (i,j,k,l)-th entry
413 const T& operator()(const unsigned& i,
414 const unsigned& j,
415 const unsigned& k,
416 const unsigned& l) const
417 {
418#ifdef RANGE_CHECKING
419 if ((i >= N1) || (j >= N2) || (k >= N3) || (l >= N4))
420 {
421 std::stringstream error_message;
422
423 error_message << "Range error: trying to access entry (" << i << ","
424 << j << "," << k << "," << l << ") "
425 << "in a fixed-size rank-four tensor of size " << N1
426 << " x " << N2 << " x " << N3 << " x " << N4 << std::endl;
427
428 throw OomphLibError(error_message.str(),
431 }
432#endif
433
434 return Data[((i * N2 + j) * N3 + k) * N4 + l];
435 }
436
437 private:
438 /// Tensor data
439 T Data[N1 * N2 * N3 * N4];
440 };
441
442
443} // namespace oomph
444
445#endif
cstr elem_len * i
Definition cfortran.h:603
A class that represents a collection of data; each Data object may contain many different individual ...
Definition nodes.h:86
Class for fixed-size matrix storing NROW x NCOL objects of type T.
unsigned nrow() const
Number of rows.
const T & operator()(const unsigned &i, const unsigned &j) const
Read access to (i,j)-th entry.
T & operator()(const unsigned &i, const unsigned &j)
Read/write access to (i,j)-th entry.
void initialise(const T &value)
Fill all entries with specified value.
FixedSizeMatrix(const T &value)
Construct and fill all entries with specified value.
FixedSizeMatrix()
Default constructor.
void fill(const T &value)
Fill all entries with specified value.
unsigned ncol() const
Number of columns.
Class for fixed-size rank-four tensor storing N1 x N2 x N3 x N4 objects of type T.
T & operator()(const unsigned &i, const unsigned &j, const unsigned &k, const unsigned &l)
Read/write access to (i,j,k,l)-th entry.
const T & operator()(const unsigned &i, const unsigned &j, const unsigned &k, const unsigned &l) const
Read access to (i,j,k,l)-th entry.
FixedSizeRankFourTensor(const T &value)
Constructor with initialisation.
void fill(const T &value)
Fill all entries with specified value.
FixedSizeRankFourTensor()
Default constructor.
Class for fixed-size rank-three tensor storing N1 x N2 x N3 objects of type T.
FixedSizeRankThreeTensor()
Default constructor.
void fill(const T &value)
Fill all entries with specified value.
FixedSizeRankThreeTensor(const T &value)
Constructor with initialisation.
T & operator()(const unsigned &i, const unsigned &j, const unsigned &k)
Read/write access to (i,j,k)-th entry.
const T & operator()(const unsigned &i, const unsigned &j, const unsigned &k) const
Read access to (i,j,k)-th entry.
Class for fixed-size vector storing N objects of type T. No resize or other STL functionality but (th...
T & operator[](const unsigned &i)
Read/write access to i-th entry.
unsigned size() const
Size of vector.
FixedSizeVector(const T &value)
Constructor with initialisation to value.
const T & operator[](const unsigned &i) const
Read access to i-th entry.
FixedSizeVector()
Constructor; no initialisiation.
FixedSizeVector(std::initializer_list< T > values)
Constructor from initializer list. Allows FixedSizeVector<double,3> = {0.0,1.0,2.0}; say.
void fill(const T &value)
Fill all entries with specified value.
An OomphLibError object which should be thrown when an run-time error is encountered....
TAdvectionDiffusionReactionElement<NREAGENT,DIM,NNODE_1D> elements are isoparametric triangular DIM-d...
DRAIG: Change all instances of (SPATIAL_DIM) to (DIM-1).