43 template<
class Function>
45 ivec3 offsetStart = {0, 0, 0},
46 ivec3 offsetEnd = {0, 0, 0}) {
54 for (
size_t k = offsetStart[2]; k < endz; k++) {
55 for (
size_t j = offsetStart[1]; j < endy; j++) {
56 for (
size_t i = offsetStart[0]; i < endx; i++) {
57 size_t index = k * nx * ny + j * nx + i;
71 template<
size_t direction,
bool parallel = false>
73 const std::function<
void(
size_t leftIndex,
size_t middleIndex,
size_t rightIndex)>&
75 ivec3 offsetStart = {0, 0, 0},
76 ivec3 offsetEnd = {0, 0, 0}) {
77 static_assert(direction < 3,
"Direction can be either 0, 1 or 2");
78 const bool xDir = direction == 0;
79 const bool yDir = direction == 1;
80 const bool zDir = direction == 2;
88 for (
size_t z = offsetStart[2]; z <
nz; z++) {
89 for (
size_t y = offsetStart[1]; y <
ny; y++) {
90 for (
size_t x = offsetStart[0]; x <
nx; x++) {
91 const size_t index = view.index(x, y, z);
92 const size_t leftIndex = view.index(x - xDir, y - yDir, z - zDir);
94 const size_t rightIndex = view.index(x + xDir, y + yDir, z + zDir);
95 function(leftIndex, index, rightIndex);
113 const std::function<
void(
size_t leftIndex,
size_t middleIndex,
size_t rightIndex)>&
115 ivec3 offsetStart = { 0, 0, 0 },
116 ivec3 offsetEnd = { 0, 0, 0 }) {
117 if (direction == 0) {
118 for_each_cell_index_with_neighbours<0>(in,
function, offsetStart, offsetEnd);
119 }
else if (direction == 1) {
120 for_each_cell_index_with_neighbours<1>(in,
function, offsetStart, offsetEnd);
121 }
else if (direction == 2) {
122 for_each_cell_index_with_neighbours<2>(in,
function, offsetStart, offsetEnd);
124 THROW(
"Unsupported direction: " << direction);
131 template<
class VariableStruct>
136 return VariableStruct();
139 template<
class VariableStruct>
142 return VariableStruct(in[0][index]);
146 template<
class VariableStruct>
149 return VariableStruct(in[0][index], in[1][index], in[2][index]);
153 template<
class VariableStruct>
156 return VariableStruct(in[0][index], in[1][index]);
159 template<
class VariableStruct>
162 return VariableStruct(in[0][index], in[1][index], in[2][index], in[3][index],
166 template<
class VariableStruct>
169 return VariableStruct(in[0][index], in[1][index], in[2][index], in[3][index]);
173 template<
class VariableStruct>
175 std::array<real*, 1>& out) {
178 out[0][index] = inAsRealPointer[0];
182 template<
class VariableStruct>
184 std::array<real*, 0>& out) {
190 template<
class VariableStruct>
192 std::array<real*, 4>& out) {
195 out[0][index] = inAsRealPointer[0];
196 out[1][index] = inAsRealPointer[1];
197 out[2][index] = inAsRealPointer[2];
198 out[3][index] = inAsRealPointer[3];
202 template<
class VariableStruct>
204 std::array<real*, 3>& out) {
207 out[0][index] = inAsRealPointer[0];
208 out[1][index] = inAsRealPointer[1];
209 out[2][index] = inAsRealPointer[2];
213 template<
class VariableStruct>
215 std::array<real*, 2>& out) {
218 out[0][index] = inAsRealPointer[0];
219 out[1][index] = inAsRealPointer[1];
225 template<
class VariableStruct>
227 std::array<real*, 5>& out) {
230 out[0][index] = inAsRealPointer[0];
231 out[1][index] = inAsRealPointer[1];
232 out[2][index] = inAsRealPointer[2];
233 out[3][index] = inAsRealPointer[3];
234 out[4][index] = inAsRealPointer[4];
248 template<
class VariableStructIn,
class VariableStructOut>
250 const std::function<VariableStructOut(
const VariableStructIn&)>&
function) {
252 std::array <
const real*,
sizeof(VariableStructIn) /
sizeof(real) > pointersIn;
253 pointersIn.fill(
nullptr);
256 THROW(
"Number of variables given does not match struct size." 257 <<
" Number of variables given: " 265 std::array < real*,
sizeof(VariableStructOut) /
sizeof(real) > pointersOut;
266 pointersOut.fill(
nullptr);
273 auto out =
function(expandVariableStruct<VariableStructIn>(pointersIn, index));
290 template<
class VariableStruct>
292 const std::function<
void(
const VariableStruct&,
size_t index)>&
function) {
293 std::array <
const real*,
sizeof(VariableStruct) /
sizeof(real) > pointersIn;
294 pointersIn.fill(
nullptr);
297 THROW(
"We expected to get " << pointersIn.size() <<
" variables, but got " <<
306 function(expandVariableStruct<VariableStruct>(pointersIn, index), index);
320 template<
class VariableStructA,
class VariableStructB>
322 const std::function<
void(
const VariableStructA&,
const VariableStructB&,
size_t index)>&
324 std::array <
const real*,
sizeof(VariableStructA) /
sizeof(real) > pointersInA;
325 pointersInA.fill(
nullptr);
326 std::array <
const real*,
sizeof(VariableStructB) /
sizeof(real) > pointersInB;
327 pointersInB.fill(
nullptr);
330 THROW(
"We expected to get " << pointersInA.size() <<
" variables, but got " <<
335 THROW(
"We expected to get " << pointersInB.size() <<
" variables, but got " <<
349 function(expandVariableStruct<VariableStructA>(pointersInA, index),
350 expandVariableStruct<VariableStructB>(pointersInB, index), index);
373 template<
class VariableStructA,
class VariableStructB>
376 const std::function <
void(
real x,
real y,
real z, VariableStructA& outA,
377 VariableStructB& outB) >& fillerFunction) {
379 std::array <
real*,
sizeof(VariableStructA) /
sizeof(real) > pointersOutA;
380 pointersOutA.fill(
nullptr);
381 std::array < real*,
sizeof(VariableStructB) /
sizeof(real) > pointersOutB;
382 pointersOutB.fill(
nullptr);
384 if (pointersOutA.size() != outA.getNumberOfVariables()) {
385 THROW(
"We expected to get " << pointersOutA.size() <<
" variables, but got " <<
386 outA.getNumberOfVariables());
389 if (pointersOutB.size() != outB.getNumberOfVariables()) {
390 THROW(
"We expected to get " << pointersOutB.size() <<
" variables, but got " <<
391 outB.getNumberOfVariables());
395 for (
size_t i = 0; i < outA.getNumberOfVariables(); i++) {
396 pointersOutA[i] = outA.getScalarMemoryArea(i)->getPointer();
399 for (
size_t i = 0; i < outB.getNumberOfVariables(); i++) {
400 pointersOutB[i] = outB.getScalarMemoryArea(i)->getPointer();
403 const size_t nx = outA.getTotalNumberOfXCells();
404 const size_t ny = outA.getTotalNumberOfYCells();
405 const size_t nz = outA.getTotalNumberOfZCells();
412 const size_t ghostX = outA.getNumberOfXGhostCells();
413 const size_t ghostY = outA.getNumberOfYGhostCells();
414 const size_t ghostZ = outA.getNumberOfZGhostCells();
416 for (
size_t k = ghostZ; k < nz - ghostZ; k++) {
417 for (
size_t j = ghostY; j < ny - ghostY; j++) {
418 for (
size_t i = ghostX; i < nx - ghostX; i++) {
419 size_t index = k * nx * ny + j * nx + i;
420 size_t midpointIndex = (k - ghostZ) * nxGrid * nyGrid
421 + (j - ghostY) * nxGrid + (i - ghostX);
422 auto midPoint = midPoints[midpointIndex];
425 fillerFunction(midPoint.x, midPoint.y, midPoint.z, a, b);
444 <
void(
real x,
real y,
real z,
size_t index,
size_t i,
size_t j,
size_t k) >&
460 for (
size_t k = ghostZ; k < nz - ghostZ; k++) {
461 for (
size_t j = ghostY; j < ny - ghostY; j++) {
462 for (
size_t i = ghostX; i < nx - ghostX; i++) {
463 size_t index = k * nx * ny + j * nx + i;
465 size_t midpointIndex = (k - ghostZ) * nxGrid * nyGrid
466 + (j - ghostY) * nxGrid + (i - ghostX);
467 auto midPoint = midPoints[midpointIndex];
469 function(midPoint.x, midPoint.y, midPoint.z, index, i - ghostX, j - ghostY,
486 const std::function <
void(
real x,
real y,
real z,
size_t index) >&
function) {
488 size_t,
size_t,
size_t) {
489 function(x, y, z, index);
512 template<
class VariableStruct>
515 const std::function <
void(
real x,
real y,
real z, VariableStruct& out) >&
518 std::array <
real*,
sizeof(VariableStruct) /
sizeof(real) > pointersOut;
519 pointersOut.fill(
nullptr);
521 if (pointersOut.size() != out.getNumberOfVariables()) {
522 THROW(
"We expected to get " << pointersOut.size() <<
" variables, but got " <<
523 out.getNumberOfVariables());
528 for (
size_t i = 0; i < out.getNumberOfVariables(); i++) {
529 pointersOut[i] = out.getScalarMemoryArea(i)->getPointer();
534 fillerFunction(x, y, z, a);
564 template<
size_t direction>
566 const std::function<
void(
size_t indexLeft,
size_t indexMiddle,
size_t indexRight)>&
569 static_assert(direction < 3,
"We only support up to three dimensions.");
570 const bool zDir = direction == 2;
571 const bool yDir = direction == 1;
572 const bool xDir = direction == 0;
578 const size_t startZ = ghostLayer * zDir;
579 const size_t startY = ghostLayer * yDir;
580 const size_t startX = ghostLayer * xDir;
582 const size_t endZ = nz - ghostLayer * zDir;
583 const size_t endY = ny - ghostLayer * yDir;
584 const size_t endX = nx - ghostLayer * xDir;
588 for (
size_t z = startZ; z < endZ; z++) {
589 for (
size_t y = startY; y < endY; y++) {
590 for (
size_t x = startX; x < endX; x++) {
591 const size_t index = z * nx * ny + y * nx + x;
592 const size_t leftIndex = (z - zDir) * nx * ny
596 const size_t rightIndex = (z + zDir) * nx * ny
600 function(leftIndex, index, rightIndex);
618 const std::function<
void(
size_t indexMiddle)>&
function) {
628 const size_t startZ = ngz;
629 const size_t startY = ngy;
630 const size_t startX = ngx;
632 const size_t endZ = nz - ngz;
633 const size_t endY = ny - ngy;
634 const size_t endX = nx - ngx;
638 for (
size_t z = startZ; z < endZ; z++) {
639 for (
size_t y = startY; y < endY; y++) {
640 for (
size_t x = startX; x < endX; x++) {
641 const size_t index = z * nx * ny + y * nx + x;
672 template<
size_t direction>
674 const std::function<
void(
size_t indexLeft,
size_t indexMiddle,
size_t indexRight)>&
676 for_each_internal_volume_index<direction>(volume,
function,
685 const std::function<
void(
size_t indexLeft,
size_t indexMiddle,
size_t indexRight)>&
689 if (direction == 0) {
690 for_each_internal_volume_index<0>(volume,
function, ghostLayer);
691 }
else if (direction == 1) {
692 for_each_internal_volume_index<1>(volume,
function, ghostLayer);
693 }
else if (direction == 2) {
694 for_each_internal_volume_index<2>(volume,
function, ghostLayer);
696 THROW(
"We only support direction 0, 1 or 2, was given: " << direction);
702 const std::function<
void(
size_t indexLeft,
size_t indexMiddle,
size_t indexRight)>&
ivec3 getDimensions() const
Definition: Grid.cpp:127
#define THROW(message)
Definition: Exception.hpp:27
const std::vector< rvec3 > & getCellMidpoints() const
Definition: Grid.cpp:148
void for_each_cell_index_with_neighbours(const Volume &in, const std::function< void(size_t leftIndex, size_t middleIndex, size_t rightIndex)> &function, ivec3 offsetStart={0, 0, 0}, ivec3 offsetEnd={0, 0, 0})
Definition: volume_foreach.hpp:72
virtual alsfvm::shared_ptr< memory::Memory< real > > & getScalarMemoryArea(size_t index)
getScalarMemoryArea gets the scalar memory area (real)
Definition: Volume.cpp:107
The Volume class represents a volume (a collection of cells with values for each cell (eg...
Definition: Volume.hpp:30
void transform_volume(const Volume &in, Volume &out, const std::function< VariableStructOut(const VariableStructIn &)> &function)
Definition: volume_foreach.hpp:249
virtual size_t getNumberOfVariables() const
getNumberOfVariables gets the number of variables used
Definition: Volume.cpp:83
virtual size_t getTotalNumberOfYCells() const
Definition: Volume.cpp:268
VariableStruct expandVariableStruct(const std::array< const real *, 0 > &in, size_t index)
Definition: volume_foreach.hpp:132
size_t nx
Definition: VolumeFactory.cpp:87
double real
Definition: types.hpp:65
virtual size_t getTotalNumberOfZCells() const
Definition: Volume.cpp:275
void saveVariableStruct(const VariableStruct &in, size_t index, std::array< real *, 1 > &out)
Definition: volume_foreach.hpp:174
virtual size_t getNumberOfXGhostCells() const
Definition: Volume.cpp:238
virtual size_t getNumberOfZGhostCells() const
Definition: Volume.cpp:254
void fill_volume(Volume &outA, Volume &outB, const grid::Grid &grid, const std::function< void(real x, real y, real z, VariableStructA &outA, VariableStructB &outB) > &fillerFunction)
Definition: volume_foreach.hpp:374
size_t ny
Definition: VolumeFactory.cpp:88
const grid::Grid & grid
Definition: NumericalFluxFactory.cpp:104
virtual size_t getNumberOfYGhostCells() const
Definition: Volume.cpp:246
#define static_assert(x, y)
Definition: types.hpp:52
void for_each_midpoint(const Volume &volume, const grid::Grid &grid, const std::function< void(real x, real y, real z, size_t index, size_t i, size_t j, size_t k) > &function)
Definition: volume_foreach.hpp:441
size_t nz
Definition: VolumeFactory.cpp:89
T y
Definition: vec3.hpp:27
void for_each_internal_volume_index(const Volume &volume, const std::function< void(size_t indexLeft, size_t indexMiddle, size_t indexRight)> &function, size_t ghostLayer)
Definition: volume_foreach.hpp:565
Various utility functions to implement the tecno flux.
Definition: types.hpp:30
virtual size_t getTotalNumberOfXCells() const
Definition: Volume.cpp:261
T x
Definition: vec3.hpp:26
void for_each_cell(const Volume &in, const std::function< void(const VariableStruct &, size_t index)> &function)
Definition: volume_foreach.hpp:291
void for_each_cell_index(const Volume &in, const Function &function, ivec3 offsetStart={0, 0, 0}, ivec3 offsetEnd={0, 0, 0})
Definition: volume_foreach.hpp:44