OpenLB 1.8.1
Loading...
Searching...
No Matches
blockLatticeFieldReductionO.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2025 Yuji (Sam) Shimojima
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 BLOCK_LATTICE_FIELD_REDUCTION_O_H
25#define BLOCK_LATTICE_FIELD_REDUCTION_O_H
26
27namespace olb {
28
29namespace field::reduction {
30
31//this filed is tag for extracting the 'core' grid area(no padding area) for the reduction, especially in GPU.
32struct TAG_CORE : public descriptors::TYPED_FIELD_BASE<int, 1> {};
33
34}
35
36namespace reduction {
37struct TAGS_U : public descriptors::TYPED_FIELD_BASE<int, 1> {};
38
40 template <typename CELL>
41 bool operator()(CELL& cell) any_platform {
42 return true;
43 }
44};
45
46template <typename TAG_FIELD>
47 struct checkBulkTag {
48 template <typename CELL>
49 bool operator()(CELL& cell) any_platform
50 {
51 return cell.template getField<TAG_FIELD>() == (int) 1;
52 }
53 };
54
55struct SumO {
56 template <typename FIELDD>
57 FIELDD operator()(FIELDD lhs, FIELDD rhs) any_platform
58 {
59 return lhs + rhs;
60 }
61
62 template <typename T>
64 {
65 T reset {};
66 return reset;
67 }
68
69#ifdef PARALLEL_MODE_MPI
70 MPI_Op forMPI() { return MPI_SUM; }
71#endif // PARALLEL_MODE_MPI
72};
73
74struct MaxO {
75 template <typename FIELDD>
76 FIELDD operator()(FIELDD lhs, FIELDD rhs) any_platform
77 {
78 // return maxv(lhs, rhs);
79 return lhs > rhs ? lhs : rhs;
80 }
81
82 template <typename T>
84 {
85 return std::numeric_limits<T>::lowest();
86 }
87
88#ifdef PARALLEL_MODE_MPI
89 MPI_Op forMPI() { return MPI_MAX; }
90#endif // PARALLEL_MODE_MPI
91};
92
93struct MinO {
94 template <typename FIELDD>
95 FIELDD operator()(FIELDD lhs, FIELDD rhs) any_platform
96 {
97 // return minv(lhs, rhs);
98 return lhs < rhs ? lhs : rhs;
99 }
100
101 template <typename T>
103 {
104 return std::numeric_limits<T>::max();
105 }
106
107#ifdef PARALLEL_MODE_MPI
108 MPI_Op forMPI() { return MPI_MIN; }
109#endif // PARALLEL_MODE_MPI
110};
111
112struct ResetO {
113 template <typename FIELDD>
114 FIELDD operator()(FIELDD lhs) any_platform
115 {
116 FIELDD reset{};
117 return reset;
118 }
119};
120
121}
122
123template <typename FIELD, typename REDUCTION_OP, typename CONDITION>
126
128
129 int getPriority() const { return 2; }
130
131 template <typename BLOCK>
132 struct type {
133 void setup(BLOCK& blockLattice) {}
134 void apply(BLOCK& blockLattice) { throw std::runtime_error("BlockLatticeFieldReductionO not implemented"); }
135 };
136
137 template <typename BLOCK>
138 void setup(BLOCK& blockLattice)
139 {
140 type<BLOCK> {}.setup(blockLattice);
141 }
142
143 template <typename BLOCK>
144 void apply(BLOCK& blockLattice)
145 {
146 type<BLOCK> {}.apply(blockLattice);
147 }
148};
149
150template <typename FIELD, typename REDUCTION_OP, typename CONDITION>
151template <typename T, typename DESCRIPTOR, Platform PLATFORM>
154
156 {
157 blockLattice.template getData<OperatorParameters<BlockLatticeFieldReductionO>>();
158 }
159
161 {
162
163 OstreamManager clout(std::cout, "Apply function in BlockLatticeFieldReductionO on CPU");
164
165 auto& parameters = blockLattice.template getData<OperatorParameters<BlockLatticeFieldReductionO>>().parameters;
166
167 const auto& blockField = blockLattice.template getField<FIELD>();
169 parameters.template get<fields::array_of<FIELD>>(); //not auto. because of considering 1 dimensional field
170
171 for (unsigned iD = 0; iD < blockField.d; iD++) {
172 if (elementField[iD] == nullptr) {
173 return;
174 }
175 else {
176 elementField[iD][0] = REDUCTION_OP {}.reset(elementField[iD][0]);
177 }
178 }
179
180 blockLattice.forSpatialLocations([&](LatticeR<DESCRIPTOR::d> latticeR) {
181 const std::size_t iCell = blockLattice.getCellId(latticeR);
182 const ConstCell<T, DESCRIPTOR> cell = blockLattice.get(iCell);
183 if (cell.template getField<field::reduction::TAG_CORE>() == (int)1) {
184 if (CONDITION {}(cell)) {
185 FieldD<T, DESCRIPTOR, FIELD> blockCellField = blockField.getRow(iCell);
186 for (unsigned iD = 0; iD < blockField.d; iD++) {
187 elementField[iD][0] = REDUCTION_OP {}(elementField[iD][0], blockCellField[iD]);
188 }
189 }
190 }
191 });
192 }
193};
194
195} // namespace olb
196
197#endif //BLOCK_LATTICE_FIELD_REDUCTION_O_H
Cell< T, DESCRIPTOR > get(CellID iCell)
Get Cell interface for index iCell.
void forSpatialLocations(F f) const
CellID getCellId(LatticeR< D > latticeR) const
Get 1D cell ID.
Implementation of BlockLattice on a concrete PLATFORM.
Highest-level interface to read-only Cell data.
Definition interface.h:36
class for marking output with some text
Plain old scalar vector.
Top level namespace for all of OpenLB.
constexpr bool isPlatformCPU(Platform platform)
Returns true if platform is equal to Platform::CPU_*.
Definition platform.h:83
OperatorScope
Block-wide operator application scopes.
@ PerBlock
Per-block application, i.e. OPERATOR::apply is passed a ConcreteBlockLattice.
#define any_platform
Define preprocessor macros for device-side functions, constant storage.
Definition platform.h:77
Base of a descriptor field of scalar TYPE.
Definition fields.h:132
Plain wrapper for list of types.
Definition meta.h:276
bool operator()(CELL &cell) any_platform
FIELDD operator()(FIELDD lhs, FIELDD rhs) any_platform
FIELDD operator()(FIELDD lhs, FIELDD rhs) any_platform
FIELDD operator()(FIELDD lhs) any_platform
FIELDD operator()(FIELDD lhs, FIELDD rhs) any_platform
bool operator()(CELL &cell) any_platform