VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
CbArray3D.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef CBARRAY3D_H
35#define CBARRAY3D_H
36
37#include <algorithm>
38#include <iomanip>
39#include <typeinfo>
40
44
47// IndexClasses
48
49// IndexerX3X2X1:
50// 4 5 6 10 11 12
51// Array ebene A 1 2 3 ebene B 7 8 9 --> vector 1 2 3 4 5 6 7 8 9 10 11 12
52// x1-reihen "liegen am stueck" im speicher
53// optimaler schleifendurchlauf
54// for(alle X3)
55// for(alle X2)
56// for(alle X1)
57class IndexerX3X2X1 // FunctorX1SortedForX1X2Plane
58{
59public:
61
62public:
63 inline std::size_t getIndex(const size_type &x1, const size_type &x2, const size_type &x3, const size_type &nx1,
64 const size_type &nx2, const size_type & /*nx3*/) const
65 {
66 return nx1 * (nx2 * x3 + x2) + x1;
67 }
68 inline std::size_t getStartIndexOfSortedArray(const size_type & /*x1*/, const size_type &x2, const size_type &x3,
69 const size_type &nx1, const size_type &nx2,
70 const size_type & /*nx3*/) const
71 {
72 return nx1 * (nx2 * x3 + x2);
73 }
74};
75
76// IndexerX1X2X3:
77// 4 5 6 10 11 12
78// Array ebene A 1 2 3 ebene B 7 8 9 -->
79// optimaler schleifendurchlauf
80// for(alle X1)
81// for(alle X2)
82// for(alle X3)
83class IndexerX1X2X3 // FunctorX3SortedForX3X2Plane
84{
85public:
87
88public:
89 inline std::size_t getIndex(const size_type &x1, const size_type &x2, const size_type &x3,
90 const size_type & /*nx1*/, const size_type &nx2, const size_type &nx3) const
91 {
92 return nx3 * (nx2 * x1 + x2) + x3;
93 }
94 inline std::size_t getStartIndexOfSortedArray(const size_type &x1, const size_type &x2, const size_type & /*x3*/
95 ,
96 const size_type & /*nx1*/, const size_type &nx2,
97 const size_type &nx3) const
98 {
99 return nx3 * (nx2 * x1 + x2);
100 }
101};
102
103// IndexerX2X1X3:
104// 4 5 6 10 11 12
105// Array ebene A 1 2 3 ebene B 7 8 9 --> vector 1 7 2 8 3 9 4 10 5 11 6 12
106// optimaler schleifendurchlauf
107// for(alle X2)
108// for(alle X1)
109// for(alle X3)
111{
112public:
114
115public:
116 inline std::size_t getIndex(const size_type &x1, const size_type &x2, const size_type &x3, const size_type &nx1,
117 const size_type & /*nx2*/, const size_type &nx3) const
118 {
119 return nx3 * (nx1 * x2 + x1) + x3;
120 }
121 inline std::size_t getStartIndexOfSortedArray(const size_type &x1, const size_type &x2, const size_type & /*x3*/
122 ,
123 const size_type &nx1, const size_type & /*nx2*/,
124 const size_type &nx3) const
125 {
126 return nx3 * (nx1 * x2 + x1);
127 }
128};
129
132// CbArray3D
143template <typename T, typename IndexClass = IndexerX3X2X1>
145{
146public:
148
149 using value_type = T;
151 using size_type = typename IndexClass::size_type;
152 using reference = typename std::vector<value_type>::reference;
153 using const_reference = typename std::vector<value_type>::const_reference;
154 using pointer = typename std::vector<value_type>::pointer;
155 using const_pointer = typename std::vector<value_type>::const_pointer;
156
157private:
158 template <typename value_type2, typename IndexClass2>
159 friend class CbArray3D;
160
161public:
162 /*=======================================================================*/
163 CbArray3D() { this->resize(0, 0, 0); }
164 /*=======================================================================*/
166 {
167 this->resize(nx1, nx2, nx3, val);
168 }
169 /*=======================================================================*/
170 CbArray3D(const size_type &nx1, const size_type &nx2, const size_type &nx3) { this->resize(nx1, nx2, nx3); }
171 /*=======================================================================*/
172 CbArray3D(const size_type &uniformDimensionSize /*nx1==nx2==nx3*/)
173 {
174 this->resize(uniformDimensionSize, uniformDimensionSize, uniformDimensionSize);
175 }
176 /*=======================================================================*/
177 // ssbernimmt vector als daten vector! (erstellt KEINE kopie!!!, vec ist anschliessend leer, da swap verwendet wird)
178 CbArray3D(std::vector<value_type> &vec, const size_type &nx1, const size_type &nx2, const size_type &nx3)
179 {
180 assert((nx1 * nx2 * nx3) == vec.size());
181 this->data.swap(vec);
182 this->resize(nx1, nx2, nx3);
183 }
184 /*=======================================================================*/
186 /*=======================================================================*/
187 template <typename value_type2>
189 {
190 // Sourcedaten kopieren
191 this->data.resize(src.data.size());
192 for (std::size_t i = 0; i < data.size(); ++i)
193 this->data[i] = src.data[i];
194 }
195 /*=======================================================================*/
196 virtual ~CbArray3D() = default;
197 /*=======================================================================*/
199 {
200 if (this == &rhs)
201 return *this;
202
203 this->nx1 = rhs.nx1;
204 this->nx2 = rhs.nx2;
205 this->nx3 = rhs.nx3;
206
207 // gespeicherte Datenelemente loeschen
208 // Laenge anpassen
209 this->data.resize(rhs.data.size());
210 // gespeicherte Datenelemente loeschen
211 this->data.clear();
212
213 // Sourcedaten kopieren
214 this->data = rhs.data;
215
216 return *this;
217 }
218 /*=======================================================================*/
219 // durch value_type2 kann man z.B. ein float array einer double array zuweisen!
220 template <typename value_type2, typename IndexClass2>
222 {
223 this->nx1 = rhs.nx1;
224 this->nx2 = rhs.nx2;
225 this->nx3 = rhs.nx3;
226
227 // gespeicherte Datenelemente loeschen
228 this->data.clear();
229 // Laenge anpassen
230 this->data.resize(rhs.data.size());
231
232 // Sourcedaten kopieren (!! koennte anderen Indexer besitzen!!! -> operator() benutzen)
233 for (int x3 = 0; x3 < this->nx3; x3++)
234 for (int x2 = 0; x2 < this->nx2; x2++)
235 for (int x1 = 0; x1 < this->nx1; x1++)
236 this->operator()(x1, x2, x3) = static_cast<value_type>(rhs.operator()(x1, x2, x3));
237
238 return *this;
239 }
240 /*=======================================================================*/
241 bool operator==(const CbArray3D &rhs) const
242 {
243 if (this == &rhs)
244 return true;
245
246 if (this->nx1 != rhs.nx1 || this->nx2 != rhs.nx2 || this->nx3 != rhs.nx3 ||
247 this->data.size() != rhs.data.size()) {
248 return false;
249 }
250
251 return std::equal(this->data.begin(), this->data.end(), rhs.data.begin(), UbEqual<value_type, value_type>());
252 }
253 /*=======================================================================*/
254 template <typename value_type2, typename IndexClass2>
256 {
257 if (this->data.size() != rhs.data.size())
258 return false;
259
260 // Sourcedaten einzeln checken (!! koennte anderen Indexer besitzen!!! -> operator() benutzen)
261 for (int x3 = 0; x3 < this->nx3; x3++)
262 for (int x2 = 0; x2 < this->nx2; x2++)
263 for (int x1 = 0; x1 < this->nx1; x1++)
264 if (!isUbEqual(this->operator()(x1, x2, x3), rhs.operator()(x1, x2, x3)))
265 return false;
266
267 return true;
268 }
269 /*=======================================================================*/
270 bool operator!=(const CbArray3D &src) const { return !(*this == src); }
271 /*=======================================================================*/
272 template <typename value_type2, typename IndexClass2>
274 {
275 return !(*this == rhs);
276 }
277 /*=======================================================================*/
278 reference operator()(const size_type &x1, const size_type &x2, const size_type &x3)
279 {
280#if !defined(VF_NO_RANGECHECK) && (defined(VF_DEBUG) || defined(VF_RANGECHECK))
281 if (!this->indicesInRange(x1, x2, x3))
283#endif
284
285 return this->data[indexer.getIndex(x1, x2, x3, nx1, nx2, nx3)];
286 }
287 /*=======================================================================*/
288 const_reference operator()(const size_type &x1, const size_type &x2, const size_type &x3) const
289 {
290#if !defined(VF_NO_RANGECHECK) && (defined(VF_DEBUG) || defined(VF_RANGECHECK))
291 if (!this->indicesInRange(x1, x2, x3))
293#endif
294
295 return this->data[indexer.getIndex(x1, x2, x3, nx1, nx2, nx3)];
296 }
297 /*=======================================================================*/
299 {
300#if !defined(VF_NO_RANGECHECK) && (defined(VF_DEBUG) || defined(VF_RANGECHECK))
301 if (!this->indicesInRange(x1, x2, x3))
303#endif
304
305 return &this->data[indexer.getStartIndexOfSortedArray(x1, x2, x3, nx1, nx2, nx3)];
306 }
307 /*=======================================================================*/
309 {
310#if !defined(VF_NO_RANGECHECK) && (defined(VF_DEBUG) || defined(VF_RANGECHECK))
311 if (!this->indicesInRange(x1, x2, x3))
313#endif
314
315 return &this->data[indexer.getStartIndexOfSortedArray(x1, x2, x3, nx1, nx2, nx3)];
316 }
317 /*=======================================================================*/
318 void setObject(const size_type &x1, const size_type &x2, const size_type &x3, const value_type &value)
319 {
320#if !defined(VF_NO_RANGECHECK) && (defined(VF_DEBUG) || defined(VF_RANGECHECK))
321 if (!this->indicesInRange(x1, x2, x3))
323#endif
324
325 this->data[indexer.getIndex(x1, x2, x3, nx1, nx2, nx3)] = value;
326 }
327 /*=======================================================================*/
328 reference getObject(const size_type &x1, const size_type &x2, const size_type &x3)
329 {
330#if !defined(VF_NO_RANGECHECK) && (defined(VF_DEBUG) || defined(VF_RANGECHECK))
331 if (!this->indicesInRange(x1, x2, x3))
333#endif
334
335 return this->data[indexer.getIndex(x1, x2, x3, nx1, nx2, nx3)];
336 }
337 /*=======================================================================*/
338 const_reference getObject(const size_type &x1, const size_type &x2, const size_type &x3) const
339 {
340 return (*this)(x1, x2, x3);
341 }
342 /*=======================================================================*/
343 bool isEmpty() const { return data.empty(); }
344 size_type getNX1() const { return this->nx1; }
345 size_type getNX2() const { return this->nx2; }
346 size_type getNX3() const { return this->nx3; }
347 /*=======================================================================*/
348 void reset(const value_type &val) { std::fill(this->data.begin(), this->data.end(), val); }
349 /*=======================================================================*/
350 std::string toString() const
351 {
352 std::stringstream text;
353 for (size_type x1 = 0; x1 < this->nx1; x1++) {
354 for (size_type x2 = 0; x2 < this->nx2; x2++) {
355 for (size_type x3 = 0; x3 < this->nx3; x3++) {
356 text << (*this)(x1, x2, x3) << ", ";
357 }
358 text << std::endl;
359 }
360 text << std::endl << std::endl;
361 }
362
363 return text.str();
364 }
365 /*=======================================================================*/
366 std::string getInfo() const
367 {
368 std::stringstream text;
369 text << "CbArray3D< storageType=" << typeid(T).name() << ", indexer=" << typeid(IndexClass).name() << " >";
370 text << "( nx1=" << this->nx1 << ", nx2=" << this->nx2 << ", nx3=" << this->nx3 << ")";
371 return text.str();
372 }
373 /*=======================================================================*/
375 {
376 this->resize(uniformDimensionSize, uniformDimensionSize, uniformDimensionSize);
377 }
378 /*=======================================================================*/
379 void resize(const size_type &nx1, const size_type &nx2, const size_type &nx3)
380 {
381 this->nx1 = nx1;
382 this->nx2 = nx2;
383 this->nx3 = nx3;
384 this->data.resize(nx1 * nx2 * nx3);
385 }
386 /*=======================================================================*/
387 void resize(const size_type &nx1, const size_type &nx2, const size_type &nx3, const value_type &val)
388 {
389 this->nx1 = nx1;
390 this->nx2 = nx2;
391 this->nx3 = nx3;
392 this->data.resize(nx1 * nx2 * nx3, val);
393 }
394 /*=======================================================================*/
395 void clear()
396 {
397 this->nx1 = 0;
398 this->nx2 = 0;
399 this->nx3 = 0;
400 this->data.clear();
401 }
402 /*=======================================================================*/
403 std::vector<value_type> &getDataVector() { return this->data; }
404 /*=======================================================================*/
405 const std::vector<value_type> &getDataVector() const { return this->data; }
406 /*=======================================================================*/
407 inline std::size_t getDataVectorIndex(const size_type &x1, const size_type &x2, const size_type &x3) const
408 {
409#if !defined(VF_NO_RANGECHECK) && (defined(VF_DEBUG) || defined(VF_RANGECHECK))
410 if (!this->indicesInRange(x1, x2, x3))
412#endif
413
414 return indexer.getIndex(x1, x2, x3, nx1, nx2, nx3);
415 }
416
417 /*=======================================================================*/
418 // success -> true
419 // else -> false
420 inline bool indicesInRange(const size_type &x1, const size_type &x2, const size_type &x3) const
421 {
422 if (x1 < 0 || x1 >= this->nx1 || x2 < 0 || x2 >= this->nx2 || x3 < 0 || x3 >= this->nx3) {
423 return false;
424 }
425 return true;
426 }
427
428protected:
429 /*=======================================================================*/
430 std::string getExceptionErrorString(const size_type &x1, const size_type &x2, const size_type &x3) const
431 {
432 std::stringstream out("index out of range - ");
433 out << "(" << x1 << "," << x2 << "," << x3 << ") not in (" << nx1 << "," << nx2 << "," << nx3 << ")";
434 return out.str();
435 }
436 /*=======================================================================*/
437
438protected:
443 std::vector<value_type> data;
444};
445
446#endif // CBARRAY3D_H
447
3D Array
Definition CbArray3D.h:145
size_type getNX3() const
Definition CbArray3D.h:346
void resize(const int &uniformDimensionSize)
Definition CbArray3D.h:374
bool operator==(const CbArray3D< value_type2, IndexClass2 > &rhs) const
Definition CbArray3D.h:255
std::vector< value_type > & getDataVector()
Definition CbArray3D.h:403
typename std::vector< value_type >::const_reference const_reference
Definition CbArray3D.h:153
IndexClass indexer_type
Definition CbArray3D.h:150
size_type getNX2() const
Definition CbArray3D.h:345
reference getObject(const size_type &x1, const size_type &x2, const size_type &x3)
Definition CbArray3D.h:328
std::vector< value_type > data
Definition CbArray3D.h:443
virtual ~CbArray3D()=default
size_type nx2
Definition CbArray3D.h:440
typename IndexClass::size_type size_type
Definition CbArray3D.h:151
std::string getInfo() const
Definition CbArray3D.h:366
indexer_type indexer
Definition CbArray3D.h:442
void clear()
Definition CbArray3D.h:395
std::string getExceptionErrorString(const size_type &x1, const size_type &x2, const size_type &x3) const
Definition CbArray3D.h:430
CbArray3D(const size_type &nx1, const size_type &nx2, const size_type &nx3, const value_type &val)
Definition CbArray3D.h:165
size_type nx1
Definition CbArray3D.h:439
bool operator!=(const CbArray3D &src) const
Definition CbArray3D.h:270
typename std::vector< value_type >::const_pointer const_pointer
Definition CbArray3D.h:155
bool indicesInRange(const size_type &x1, const size_type &x2, const size_type &x3) const
Definition CbArray3D.h:420
std::size_t getDataVectorIndex(const size_type &x1, const size_type &x2, const size_type &x3) const
Definition CbArray3D.h:407
void reset(const value_type &val)
Definition CbArray3D.h:348
CbArray3D(const size_type &uniformDimensionSize)
Definition CbArray3D.h:172
SPtr< CbArray3D< T, IndexClass > > CbArray3DPtr
Definition CbArray3D.h:147
const_reference operator()(const size_type &x1, const size_type &x2, const size_type &x3) const
Definition CbArray3D.h:288
CbArray3D(const size_type &nx1, const size_type &nx2, const size_type &nx3)
Definition CbArray3D.h:170
const_reference getObject(const size_type &x1, const size_type &x2, const size_type &x3) const
Definition CbArray3D.h:338
reference operator()(const size_type &x1, const size_type &x2, const size_type &x3)
Definition CbArray3D.h:278
CbArray3D(const CbArray3D< value_type2 > &src)
Definition CbArray3D.h:188
size_type nx3
Definition CbArray3D.h:441
bool operator!=(const CbArray3D< value_type2, IndexClass2 > &rhs) const
Definition CbArray3D.h:273
void resize(const size_type &nx1, const size_type &nx2, const size_type &nx3)
Definition CbArray3D.h:379
size_type getNX1() const
Definition CbArray3D.h:344
const_pointer getStartAdressOfSortedArray(const size_type &x1, const size_type &x2, const size_type &x3) const
Definition CbArray3D.h:308
typename std::vector< value_type >::pointer pointer
Definition CbArray3D.h:154
void resize(const size_type &nx1, const size_type &nx2, const size_type &nx3, const value_type &val)
Definition CbArray3D.h:387
std::string toString() const
Definition CbArray3D.h:350
CbArray3D & operator=(const CbArray3D< value_type2, IndexClass2 > &rhs)
Definition CbArray3D.h:221
bool operator==(const CbArray3D &rhs) const
Definition CbArray3D.h:241
pointer getStartAdressOfSortedArray(const size_type &x1, const size_type &x2, const size_type &x3)
Definition CbArray3D.h:298
CbArray3D(std::vector< value_type > &vec, const size_type &nx1, const size_type &nx2, const size_type &nx3)
Definition CbArray3D.h:178
typename std::vector< value_type >::reference reference
Definition CbArray3D.h:152
const std::vector< value_type > & getDataVector() const
Definition CbArray3D.h:405
CbArray3D & operator=(const CbArray3D &rhs)
Definition CbArray3D.h:198
CbArray3D(const CbArray3D &src)
Definition CbArray3D.h:185
void setObject(const size_type &x1, const size_type &x2, const size_type &x3, const value_type &value)
Definition CbArray3D.h:318
bool isEmpty() const
Definition CbArray3D.h:343
std::size_t getStartIndexOfSortedArray(const size_type &x1, const size_type &x2, const size_type &, const size_type &, const size_type &nx2, const size_type &nx3) const
Definition CbArray3D.h:94
size_t size_type
Definition CbArray3D.h:86
std::size_t getIndex(const size_type &x1, const size_type &x2, const size_type &x3, const size_type &, const size_type &nx2, const size_type &nx3) const
Definition CbArray3D.h:89
size_t size_type
Definition CbArray3D.h:113
std::size_t getStartIndexOfSortedArray(const size_type &x1, const size_type &x2, const size_type &, const size_type &nx1, const size_type &, const size_type &nx3) const
Definition CbArray3D.h:121
std::size_t getIndex(const size_type &x1, const size_type &x2, const size_type &x3, const size_type &nx1, const size_type &, const size_type &nx3) const
Definition CbArray3D.h:116
std::size_t getIndex(const size_type &x1, const size_type &x2, const size_type &x3, const size_type &nx1, const size_type &nx2, const size_type &) const
Definition CbArray3D.h:63
std::size_t getStartIndexOfSortedArray(const size_type &, const size_type &x2, const size_type &x3, const size_type &nx1, const size_type &nx2, const size_type &) const
Definition CbArray3D.h:68
size_t size_type
Definition CbArray3D.h:60
std::shared_ptr< T > SPtr
UbTypeOp< typenameUbDuoT< N, UbDuo< A, B > >::ResultT >::RefT val(UbDuo< A, B > &d)
Definition UbTuple.h:393
#define UB_THROW(e)
Definition UbException.h:78
#define UB_EXARGS
Definition UbException.h:73
bool isUbEqual(const T1 &a, const T2 &b)
Definition UbEqual.h:259