MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
lbmbcellcollector.h
Go to the documentation of this file.
1// Copyright (C) 2024 The m-AIA AUTHORS
2//
3// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
4//
5// SPDX-License-Identifier: LGPL-3.0-only
6
7#ifndef LBMBCOLLECTOR_H_
8#define LBMBCOLLECTOR_H_
9
10#include <vector>
11
12#include "INCLUDE/maiamacro.h"
13#include "INCLUDE/maiatypes.h"
14#include "MEMORY/container.h"
15#include "compiler_config.h"
16#include "lbcellcollector.h"
17
18// The following macro enables the "Structure-of-Arrays" memory layout for multi-dimensional node
19// variables. This might be beneficial for GPU computations. Default is "Array-of-Structures".
20// Examples (for nodes nN with four children cM each)
21// Array-of-Structures (AOS): n0c0, n0c1, n0c2, n0c3, n1c0, n1c1, n1c2, n1c3, n2c0, n2c1, ...
22// Structure-of-Arrays (SOA): n0c0, n1c0, n2c0, n3c0, ..., n0c1, n1c1, n2c1, n3c1, ..., n0c2, ...
23// #define LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
24
25// The macro 'LBCOLLECTOR_SANITY_CHECKS_ACCESSORS' enables (potentially very expensive) sanity
26// checks for all accessors. It is enabled for build type "extra_debug".
27#ifdef MAIA_EXTRA_DEBUG
28#define LBCOLLECTOR_SANITY_CHECKS_ACCESSORS
29#endif
30
31// Sanity-checking macros for accessors
32#if defined(LBCOLLECTOR_SANITY_CHECKS_ACCESSORS) || defined(MAIA_ASSERT_ACCESSORS)
33#define ENSURE_VALID_ID_ACCESSOR(id) \
34 do { \
35 MAIA_CONTAINER_ENSURE_VALID_ID(id); \
36 } while(false)
37#define ENSURE_VALID_VARIABLE_ID_ACCESSOR(id) \
38 do { \
39 MAIA_CONTAINER_ENSURE( \
40 id >= 0 && id < noVariables(), \
41 "variable id = " + std::to_string(id) + " is out-of-bounds [0, " + std::to_string(noVariables()) + ")", AT_); \
42 } while(false)
43#define ENSURE_VALID_DISTANCE_ID_ACCESSOR(id) \
44 do { \
45 MAIA_CONTAINER_ENSURE( \
46 id >= 0 && id < noDistances(), \
47 "distance id = " + std::to_string(id) + " is out-of-bounds [0, " + std::to_string(noDistances()), AT_); \
48 } while(false)
49#define ENSURE_VALID_DIM_ID_ACCESSOR(dim) \
50 do { \
51 MAIA_CONTAINER_ENSURE(dim >= 0 && dim < nDim, \
52 "dim = " + std::to_string(dim) + " is out-of-bounds [0, " + std::to_string(nDim) + ")", \
53 AT_); \
54 } while(false)
55#define ENSURE_VALID_PROPERTY_ACCESSOR(p) \
56 do { \
57 MAIA_CONTAINER_ENSURE(p != LbCell::NumProperties, "Invalid property", AT_); \
58 } while(false)
59
60#else
61#define ENSURE_VALID_ID_ACCESSOR(id) \
62 do { \
63 } while(false)
64#define ENSURE_VALID_VARIABLE_ID_ACCESSOR(id) \
65 do { \
66 } while(false)
67#define ENSURE_VALID_DISTANCE_ID_ACCESSOR(id) \
68 do { \
69 } while(false)
70#define ENSURE_VALID_DIM_ID_ACCESSOR(id) \
71 do { \
72 } while(false)
73#define ENSURE_VALID_PROPERTY_ACCESSOR(dir) \
74 do { \
75 } while(false)
76#endif
77
78
79// Namespace for auxiliary functions/classes
80namespace maia::lb::collector {
81
82// Type traits for invalid values. These values are used to initialize/erase nodes.
83// Already defined in LbCellCollector.
84// template <class T> using Invalid = maia::lb::collector::Invalid<T>;
85
87template <MInt nDim>
88class LbMbCellCollector : public maia::container::Container<LbMbCellCollector<nDim>, Invalid> {
89 // Necessary for CRTP
91
92 // Make base class functions known to use without this pointer
95 template <class T>
96 using Storage = typename Base::template Storage<T>;
97
98 public:
99 // Types
100 // template <class T> using Invalid = typename maia::lb::collector::Invalid<T>;
101
102 // Constructors
104 constexpr LbMbCellCollector() = default;
105
106 // Ensure that base class method is found when called from outside
107 using Base::copyData;
108 using Base::fill_invalid;
109 using Base::reset;
110
111 // Accessors
112 // TODO labels:LB Access distance via bndryCellId, setId and distanceId
113 // (Or find another way to distinquish between boundaries of different sets ...)
114
115 MInt& cellId(const MInt id);
116 MInt cellId(const MInt id) const;
117 MFloat& distance(const MInt id, const MInt did);
118 MFloat distance(const MInt id, const MInt did) const;
119 MFloat& velocity(const MInt id, const MInt dim);
120 MFloat velocity(const MInt id, const MInt dim) const;
121 MFloat& force(const MInt id, const MInt did, const MInt dim);
122 MFloat force(const MInt id, const MInt did, const MInt dim) const;
123 MFloat& surfaceCenter(const MInt id, const MInt did, const MInt dim);
124 MFloat surfaceCenter(const MInt id, const MInt did, const MInt dim) const;
125 MFloat& cellCenter(const MInt id, const MInt dim);
126 MFloat cellCenter(const MInt id, const MInt dim) const;
127 MFloat& normal(const MInt id, const MInt dim);
128 MFloat normal(const MInt id, const MInt dim) const;
129 MFloat& density(const MInt id);
130 MFloat density(const MInt id) const;
131
132 // Setter
133 void setVelocity(const MInt id, const std::array<MFloat, nDim>& velocity);
134
135 // Adder
136 void addVelocity(const MInt id, const std::array<MFloat, nDim>& velocity);
137
139 constexpr MInt noDistances() const { return m_noDistances; }
141
143 constexpr MInt noForces() const { return m_noDistances; }
145
147 constexpr MInt noDimensions() const { return nDim; }
148
149 private:
150 // Methods required by base class for CRTP
151 void reset();
152 void invalidate(const MInt begin, const MInt end);
153 template <class Functor, class T>
154 void rawCopyGeneric(Functor&& c, const T& source, const MInt begin, const MInt end, const MInt destination);
155
156 // Properties
158
159 // Data containers
168};
169
171template <MInt nDim>
173 resetStorage(1, m_cellId);
174 resetStorage(noDistances(), m_distances);
175 resetStorage(nDim, m_velocities);
176 resetStorage(nDim * noDistances(), m_forces);
177 resetStorage(nDim * noDistances(), m_surfaceCenters);
178 resetStorage(nDim, m_cellCenters);
179 resetStorage(nDim, m_normal);
180 resetStorage(1, m_density);
181}
182
183//---------------------------------ACCESSORS------------------------------------
184
186template <MInt nDim>
188// Prevent accidental compilation without support for SoA layout
189#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
190#error Missing implementation for structure-of-arrays memory layout.
191#endif
192 ENSURE_VALID_ID_ACCESSOR(id);
193 return m_cellId[id];
194}
196template <MInt nDim>
198// Prevent accidental compilation without support for SoA layout
199#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
200#error Missing implementation for structure-of-arrays memory layout.
201#endif
202 ENSURE_VALID_ID_ACCESSOR(id);
203 return m_cellId[id];
204}
205
207template <MInt nDim>
209// Prevent accidental compilation without support for SoA layout
210#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
211#error Missing implementation for structure-of-arrays memory layout.
212#endif
213 ENSURE_VALID_ID_ACCESSOR(id);
214 ENSURE_VALID_DISTANCE_ID_ACCESSOR(did);
215 return m_distances[id * noDistances() + did];
216}
218template <MInt nDim>
220// Prevent accidental compilation without support for SoA layout
221#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
222#error Missing implementation for structure-of-arrays memory layout.
223#endif
224 ENSURE_VALID_ID_ACCESSOR(id);
225 ENSURE_VALID_DISTANCE_ID_ACCESSOR(did);
226 return m_distances[id * noDistances() + did];
227}
228
230template <MInt nDim>
232// Prevent accidental compilation without support for SoA layout
233#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
234#error Missing implementation for structure-of-arrays memory layout.
235#endif
236 ENSURE_VALID_ID_ACCESSOR(id);
237 ENSURE_VALID_DIM_ID_ACCESSOR(dim);
238 return m_velocities[id * nDim + dim];
239}
241template <MInt nDim>
243// Prevent accidental compilation without support for SoA layout
244#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
245#error Missing implementation for structure-of-arrays memory layout.
246#endif
247 ENSURE_VALID_ID_ACCESSOR(id);
248 ENSURE_VALID_DIM_ID_ACCESSOR(dim);
249 return m_velocities[id * nDim + dim];
250}
251// Setter for velocities
252template <MInt nDim>
253void LbMbCellCollector<nDim>::setVelocity(const MInt id, const std::array<MFloat, nDim>& velocity) {
254// Prevent accidental compilation without support for SoA layout
255#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
256#error Missing implementation for structure-of-arrays memory layout.
257#endif
258 ENSURE_VALID_ID_ACCESSOR(id);
259 for(MInt n = 0; n < nDim; n++) {
260 m_velocities[id * nDim + n] = velocity[n];
261 }
262}
263// Adder for velocities
264template <MInt nDim>
265void LbMbCellCollector<nDim>::addVelocity(const MInt id, const std::array<MFloat, nDim>& velocity) {
266// Prevent accidental compilation without support for SoA layout
267#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
268#error Missing implementation for structure-of-arrays memory layout.
269#endif
270 ENSURE_VALID_ID_ACCESSOR(id);
271 for(MInt n = 0; n < nDim; n++) {
272 m_velocities[id * nDim + n] += velocity[n];
273 }
274}
275
277template <MInt nDim>
278MFloat& LbMbCellCollector<nDim>::force(const MInt id, const MInt did, const MInt dim) {
279// Prevent accidental compilation without support for SoA layout
280#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
281#error Missing implementation for structure-of-arrays memory layout.
282#endif
283 ENSURE_VALID_ID_ACCESSOR(id);
284 ENSURE_VALID_DIM_ID_ACCESSOR(dim);
285 ENSURE_VALID_DISTANCE_ID_ACCESSOR(did);
286 return m_forces[id * nDim * noDistances() + nDim * did + dim];
287}
289template <MInt nDim>
290MFloat LbMbCellCollector<nDim>::force(const MInt id, const MInt did, const MInt dim) const {
291// Prevent accidental compilation without support for SoA layout
292#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
293#error Missing implementation for structure-of-arrays memory layout.
294#endif
295 ENSURE_VALID_ID_ACCESSOR(id);
296 ENSURE_VALID_DIM_ID_ACCESSOR(dim);
297 ENSURE_VALID_DISTANCE_ID_ACCESSOR(did);
298 return m_forces[id * nDim * noDistances() + nDim * did + dim];
299}
300
302template <MInt nDim>
304// Prevent accidental compilation without support for SoA layout
305#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
306#error Missing implementation for structure-of-arrays memory layout.
307#endif
308 ENSURE_VALID_ID_ACCESSOR(id);
309 ENSURE_VALID_DIM_ID_ACCESSOR(dim);
310 ENSURE_VALID_DISTANCE_ID_ACCESSOR(did);
311 return m_surfaceCenters[id * nDim * noDistances() + nDim * did + dim];
312}
314template <MInt nDim>
315MFloat LbMbCellCollector<nDim>::surfaceCenter(const MInt id, const MInt did, const MInt dim) const {
316// Prevent accidental compilation without support for SoA layout
317#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
318#error Missing implementation for structure-of-arrays memory layout.
319#endif
320 ENSURE_VALID_ID_ACCESSOR(id);
321 ENSURE_VALID_DIM_ID_ACCESSOR(dim);
322 ENSURE_VALID_DISTANCE_ID_ACCESSOR(did);
323 return m_surfaceCenters[id * nDim * noDistances() + nDim * did + dim];
324}
325
327template <MInt nDim>
329// Prevent accidental compilation without support for SoA layout
330#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
331#error Missing implementation for structure-of-arrays memory layout.
332#endif
333 ENSURE_VALID_ID_ACCESSOR(id);
334 ENSURE_VALID_DIM_ID_ACCESSOR(dim);
335 return m_cellCenters[id * nDim + dim];
336}
338template <MInt nDim>
340// Prevent accidental compilation without support for SoA layout
341#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
342#error Missing implementation for structure-of-arrays memory layout.
343#endif
344 ENSURE_VALID_ID_ACCESSOR(id);
345 ENSURE_VALID_DIM_ID_ACCESSOR(dim);
346 return m_cellCenters[id * nDim + dim];
347}
348
350template <MInt nDim>
352// Prevent accidental compilation without support for SoA layout
353#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
354#error Missing implementation for structure-of-arrays memory layout.
355#endif
356 ENSURE_VALID_ID_ACCESSOR(id);
357 ENSURE_VALID_DIM_ID_ACCESSOR(dim);
358 return m_normal[id * nDim + dim];
359}
361template <MInt nDim>
363// Prevent accidental compilation without support for SoA layout
364#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
365#error Missing implementation for structure-of-arrays memory layout.
366#endif
367 ENSURE_VALID_ID_ACCESSOR(id);
368 ENSURE_VALID_DIM_ID_ACCESSOR(dim);
369 return m_normal[id * nDim + dim];
370}
371
373template <MInt nDim>
375// Prevent accidental compilation without support for SoA layout
376#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
377#error Missing implementation for structure-of-arrays memory layout.
378#endif
379 ENSURE_VALID_ID_ACCESSOR(id);
380 return m_density[id];
381}
383template <MInt nDim>
385// Prevent accidental compilation without support for SoA layout
386#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
387#error Missing implementation for structure-of-arrays memory layout.
388#endif
389 ENSURE_VALID_ID_ACCESSOR(id);
390 return m_density[id];
391}
392
393
394//---------------------------CRTP COLLECTOR STUFF------------------------------
395
397template <MInt nDim>
398void LbMbCellCollector<nDim>::invalidate(const MInt begin, const MInt end) {
399// Prevent accidental compilation without support for SoA layout
400#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
401#error Missing implementation for structure-of-arrays memory layout.
402#endif
403 fill_invalid(m_cellId, begin, end);
404 fill_invalid(m_distances, begin, end, noDistances());
405 fill_invalid(m_velocities, begin, end, nDim);
406 fill_invalid(m_forces, begin, end, nDim * noDistances(), 0.0);
407 fill_invalid(m_surfaceCenters, begin, end, nDim * noDistances());
408 fill_invalid(m_cellCenters, begin, end, nDim);
409 fill_invalid(m_normal, begin, end, nDim);
410 fill_invalid(m_density, begin, end);
411}
412
414template <MInt nDim>
415template <class Functor, class T>
416void LbMbCellCollector<nDim>::rawCopyGeneric(Functor&& c, const T& source, const MInt begin, const MInt end,
417 const MInt destination) {
418// Prevent accidental compilation without support for SoA layout
419#ifdef LBMBCOLLECTOR_SOA_MEMORY_LAYOUT
420#error Missing implementation for structure-of-arrays memory layout.
421#endif
422 copyData(source.m_cellId, m_cellId, c, begin, end, destination, 1);
423 copyData(source.m_distances, m_distances, c, begin, end, destination, noDistances());
424 coptData(source.m_velocities, m_velocities, c, begin, end, destination, nDim);
425 copyData(source.m_forces, m_forces, c, begin, end, destination, nDim * noDistances());
426 copyData(source.m_surfaceCenters, m_surfaceCenters, c, begin, end, destination, nDim * noDistances());
427 copyData(source.m_cellCenters, m_cellCenters, c, begin, end, destination, nDim);
428 copyData(source.m_normal, m_normal, c, begin, end, destination, nDim);
429 copyData(source.m_density, m_density, c, begin, end, destination, 1);
430}
431
432} // namespace maia::lb::collector
433
434
435// Undefine macros that should not be used outside this file
436#undef LBCOLLECTOR_SANITY_CHECKS_ACCESSORS
437#undef ENSURE_VALID_ID_ACCESSOR
438#undef ENSURE_VALID_VARIABLE_ID_ACCESSOR
439#undef ENSURE_VALID_DISTANCES_ID_ACCESSOR
440#undef ENSURE_VALID_PROPERTY_ACCESSOR
441
442#endif // ifndef LBMBCOLLECTOR_H_
void copyData(const Container_ &source, Container_ &target, Functor &&f, const MInt begin, const MInt end, const MInt dest, const MInt solverSize=1)
Copy [begin, end) range with given solver size from source to dest position of target.
Definition: container.h:138
void fill_invalid(Container_ &c, const MInt begin, const MInt end, const MInt solverSize=1, const T value=Invalid< T >::value())
Definition: container.h:131
void resetStorage(const MInt n, Storage< T > &c)
Create new container with given size and replace original one.
Definition: container.h:420
void reset(const MInt capacity)
Reset tree, re-create data structures with given capacity, and set size to zero.
Definition: container.h:187
Class that represents LB cell collector.
MFloat & surfaceCenter(const MInt id, const MInt did, const MInt dim)
Accessor for surfaceCenter.
void rawCopyGeneric(Functor &&c, const T &source, const MInt begin, const MInt end, const MInt destination)
Helper function for rawCopy(). Destination may refer to beginning or end of target range.
void noDistances(const MInt noDistances)
void reset()
Reset tree, re-create data structures with given capacity, and set size to zero.
MFloat & distance(const MInt id, const MInt did)
Accessor for distances.
void addVelocity(const MInt id, const std::array< MFloat, nDim > &velocity)
constexpr MInt noDimensions() const
Number of dimensions.
constexpr LbMbCellCollector()=default
Default c'tor does nothing.
typename Base::template Storage< T > Storage
MFloat & velocity(const MInt id, const MInt dim)
Accessor for velocities.
void invalidate(const MInt begin, const MInt end)
Erase range of nodes such that they contain no sensible values anymore.
MFloat & normal(const MInt id, const MInt dim)
Accessor for surface normal.
void noForces(const MInt noDistances)
MFloat & cellCenter(const MInt id, const MInt dim)
Accessor for cellCenter.
MFloat & density(const MInt id)
Accessor for density.
void setVelocity(const MInt id, const std::array< MFloat, nDim > &velocity)
MFloat & force(const MInt id, const MInt did, const MInt dim)
Accessor for forces.
constexpr MInt noForces() const
Number of forces.
constexpr MInt noDistances() const
Number of distances.
MInt & cellId(const MInt id)
Accessor for cellId.
int32_t MInt
Definition: maiatypes.h:62
double MFloat
Definition: maiatypes.h:52
MInt id
Definition: maiatypes.h:71