FieldOps.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2019-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "Pstream.H"
29#include "ops.H"
30#include <algorithm>
31
32// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
33
34template<class Tout, class T1, class UnaryOp>
36(
37 Field<Tout>& result,
38 const Field<T1>& a,
39 const UnaryOp& op
40)
41{
42 #ifdef FULLDEBUG
43 if (result.size() != a.size())
44 {
46 << "Field sizes do not match: " << result.size() << " ("
47 << a.size() << ')' << nl
48 << abort(FatalError);
49 }
50 #endif
51
52 std::transform(a.cbegin(), a.cend(), result.begin(), op);
53}
54
55
56template<class Tout, class T1, class T2, class BinaryOp>
58(
59 Field<Tout>& result,
60 const Field<T1>& a,
61 const Field<T2>& b,
62 const BinaryOp& bop
63)
64{
65 #ifdef FULLDEBUG
66 if (result.size() != a.size() || result.size() != b.size())
67 {
69 << "Field sizes do not match: " << result.size() << " ("
70 << a.size() << ' ' << b.size() << ')' << nl
71 << abort(FatalError);
72 }
73 #endif
74
75 std::transform(a.cbegin(), a.cend(), b.cbegin(), result.begin(), bop);
76}
77
78
79template<class T, class BinaryOp>
81(
82 Field<T>& result,
83 const Field<T>& a,
84 const Field<T>& b,
85 const BinaryOp& bop
86)
87{
88 #ifdef FULLDEBUG
89 if (result.size() != a.size() || result.size() != b.size())
90 {
92 << "Field sizes do not match: " << result.size() << " ("
93 << a.size() << ' ' << b.size() << ')' << nl
94 << abort(FatalError);
95 }
96 #endif
97
98 forAll(result, i)
99 {
100 result[i] = bop(a[i], b[i]) ? a[i] : b[i];
101 }
102}
103
104
105template<class T, class BoolListType, class FlipOp>
107(
108 Field<T>& result,
109 const BoolListType& cond,
110 const Field<T>& a,
111 const Field<T>& b,
112 const FlipOp& flip
113)
114{
115 #ifdef FULLDEBUG
116 if (result.size() != a.size() || result.size() != b.size())
117 {
119 << "Field sizes do not match: " << result.size() << " ("
120 << a.size() << ' ' << b.size() << ')' << nl
121 << abort(FatalError);
122 }
123 #endif
124
125 forAll(result, i)
126 {
127 result[i] = flip(cond[i]) ? a[i] : b[i];
128 }
129}
130
131
132template<class T, class FlipOp>
134(
135 Field<T>& result,
136 const bitSet& cond,
137 const Field<T>& a,
138 const Field<T>& b,
139 const FlipOp& flip
140)
141{
142 #ifdef FULLDEBUG
143 if (result.size() != a.size() || result.size() != b.size())
144 {
146 << "Field sizes do not match: " << result.size() << " ("
147 << a.size() << ' ' << b.size() << ')' << nl
148 << abort(FatalError);
149 }
150 #endif
151
152 forAll(result, i)
153 {
154 result[i] = flip(cond[i]) ? a[i] : b[i];
155 }
156}
157
158
159template<class T1, class T2>
161(
162 const Field<T1>& vals,
163 const Field<T2>& data
164)
165{
166 Tuple2<T1,T2> result(pTraits<T1>::max, Zero);
167
168 const label i = findMin(vals);
169 if (i != -1)
170 {
171 result.first() = vals[i];
172 result.second() = data[i];
173 }
174
175 Pstream::combineAllGather(result, minFirstEqOp<T1>());
176 return result;
177}
178
179
180template<class T1, class T2>
182(
183 const Field<T1>& vals,
184 const Field<T2>& data
185)
186{
187 Tuple2<T1,T2> result(pTraits<T1>::min, Zero);
188
189 const label i = findMax(vals);
190 if (i != -1)
191 {
192 result.first() = vals[i];
193 result.second() = data[i];
194 }
195
196 Pstream::combineAllGather(result, maxFirstEqOp<T1>());
197 return result;
198}
199
200
201// ************************************************************************* //
Generic templated field type.
Definition: Field.H:82
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: Tuple2.H:58
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:364
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:343
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:66
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
void assign(Field< Tout > &result, const Field< T1 > &a, const UnaryOp &op)
Populate a field as the result of a unary operation on an input.
Definition: FieldOps.C:36
void ternarySelect(Field< T > &result, const BoolListType &cond, const Field< T > &a, const Field< T > &b, const FlipOp &flip)
Definition: FieldOps.C:107
Tuple2< T1, T2 > findMaxData(const Field< T1 > &vals, const Field< T2 > &data)
Locate the max value in a field and return it and associated data.
Tuple2< T1, T2 > findMinData(const Field< T1 > &vals, const Field< T2 > &data)
Locate the min value in a field and return it and associated data.
void ternary(Field< T > &result, const Field< T > &a, const Field< T > &b, const BinaryOp &bop)
Definition: FieldOps.C:81
label findMin(const ListType &input, label start=0)
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
label findMax(const ListType &input, label start=0)
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
volScalarField & b
Definition: createFields.H:27
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333