OpenLB 1.7
Loading...
Searching...
No Matches
cellIndexListD.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2020 Adrian Kummerlaender
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef CELL_INDEX_LIST_D_H
25#define CELL_INDEX_LIST_D_H
26
27#include "fieldArrayD.h"
28#include "utilities/aliases.h"
29#include "serializer.h"
30
31#include <vector>
32#include <numeric>
33#include <algorithm>
34#include <type_traits>
35
36
37namespace olb {
38
39
41
48template <typename T, typename DESCRIPTOR, typename... FIELDS>
50private:
51 std::size_t _count;
52 std::size_t _capacity;
53 MultiFieldArrayD<T,DESCRIPTOR,Platform::CPU_SISD,descriptors::CELL_ID,FIELDS...> _fields;
54
55public:
56 CellIndexListD(std::size_t capacity=64):
57 _count(0),
58 _capacity(capacity),
59 _fields(capacity)
60 { }
61
62 CellIndexListD(std::vector<CellID>&& indices):
63 _capacity(indices.size()),
64 _fields(_capacity)
65 {
66 for (std::size_t i=0; i < indices.size(); ++i) {
67 _fields.template get<descriptors::CELL_ID>()[0][i] = indices[i];
68 }
69 }
70
71 std::size_t size() const {
72 return _count;
73 };
74
76 std::size_t append(std::size_t iCell);
77
79
83 void sort();
84
86 template <typename FIELD>
87 auto getFieldPointer(std::size_t index)
88 {
89 return _fields.template get<FIELD>().getRowPointer(index);
90 }
91
93 template <typename FIELD>
94 auto getField(std::size_t index) const
95 {
96 return _fields.template getField<FIELD>(index);
97 }
99 template <typename FIELD>
100 void setField(std::size_t index, const FieldD<T,DESCRIPTOR,FIELD>& v)
101 {
102 return _fields.template setField<FIELD>(index, v);
103 }
104
105 template <typename FIELD>
106 const auto& getFieldArray() const
107 {
108 return _fields.template get<FIELD>();
109 }
110
111 std::size_t getNblock() const override;
112 std::size_t getSerializableSize() const override;
113 bool* getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode) override;
114
115};
116
117template <typename T, typename DESCRIPTOR, typename... FIELDS>
119{
120 if (_count == _capacity) {
121 _capacity *= 2;
122 _fields.resize(_capacity);
123 }
124 _fields.template get<descriptors::CELL_ID>()[0][_count] = iCell;
125 return _count++;
126}
127
128template <typename T, typename DESCRIPTOR, typename... FIELDS>
130{
131 auto& cell_id = _fields.template get<descriptors::CELL_ID>()[0];
132 std::vector<std::size_t> permutation(_count);
133 std::iota(permutation.begin(), permutation.end(), 0);
134 std::sort(permutation.begin(), permutation.end(), [&cell_id](auto i, auto j) {
135 return cell_id[i] < cell_id[j];
136 });
137 std::vector<bool> swapped(_count, false);
138 for (std::size_t i=0; i < _count; ++i) {
139 if (swapped[i]) {
140 continue;
141 }
142 swapped[i] = true;
143 std::size_t prev_j = i;
144 std::size_t next_j = permutation[i];
145 while (prev_j != next_j && !swapped[next_j]) {
146 _fields.swap(prev_j, next_j);
147 swapped[next_j] = true;
148 prev_j = next_j;
149 next_j = permutation[next_j];
150 }
151 }
152}
153
154template <typename T, typename DESCRIPTOR, typename... FIELDS>
156{
157 return 2
158 + _fields.getNblock();
159}
160
161template <typename T, typename DESCRIPTOR, typename... FIELDS>
163{
164 return 2 * sizeof(std::size_t)
165 + _fields.getSerializableSize();
166}
167
168template <typename T, typename DESCRIPTOR, typename... FIELDS>
169bool* CellIndexListD<T,DESCRIPTOR,FIELDS...>::getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode)
170{
171 std::size_t currentBlock = 0;
172 bool* dataPtr = nullptr;
173
174 registerVar(iBlock, sizeBlock, currentBlock, dataPtr, _count);
175 registerVar(iBlock, sizeBlock, currentBlock, dataPtr, _capacity);
176 if (loadingMode && iBlock == 2) {
177 _fields.resize(_capacity);
178 }
179 registerSerializableOfConstSize(iBlock, sizeBlock, currentBlock, dataPtr, _fields, loadingMode);
180
181 return dataPtr;
182}
183
184}
185
186#endif
List of cell indices and associated field data.
const auto & getFieldArray() const
std::size_t size() const
bool * getBlock(std::size_t iBlock, std::size_t &sizeBlock, bool loadingMode) override
Returns the address of the i-th block and its size.
auto getField(std::size_t index) const
Return copy of FIELD data at index.
std::size_t getNblock() const override
Returns the number of blocks.
void sort()
Ascending sort of cell indices.
std::size_t getSerializableSize() const override
Returns the binary size of the data to be saved.
auto getFieldPointer(std::size_t index)
Return pointer to FIELD at index.
std::size_t append(std::size_t iCell)
Append cell index and allocate attached field data.
void setField(std::size_t index, const FieldD< T, DESCRIPTOR, FIELD > &v)
Set FIELD data at index.
CellIndexListD(std::size_t capacity=64)
CellIndexListD(std::vector< CellID > &&indices)
Storage for a fixed set of static FIELDS and arbitrary custom fields.
Base class for serializable objects of constant size. For dynamic size use BufferSerializable.
Definition serializer.h:145
Plain old scalar vector.
Definition vector.h:47
Top level namespace for all of OpenLB.