Loading...
Searching...
No Matches
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\*---------------------------------------------------------------------------*/
28#include "Pstream.H"
29#include "ops.H"
30
31// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
32
33template<class Tout, class T1, class UnaryOp>
35(
36 Field<Tout>& result,
37 const Field<T1>& a,
38 const UnaryOp& op
39)
40{
41 #ifdef FULLDEBUG
42 if (result.size() != a.size())
43 {
45 << "Field sizes do not match: " << result.size() << " ("
46 << a.size() << ')' << nl
47 << abort(FatalError);
48 }
49 #endif
50
51 std::transform(a.cbegin(), a.cend(), result.begin(), op);
52}
53
54
55template<class Tout, class T1, class T2, class BinaryOp>
57(
58 Field<Tout>& result,
59 const Field<T1>& a,
60 const Field<T2>& b,
61 const BinaryOp& bop
62)
63{
64 #ifdef FULLDEBUG
65 if (result.size() != a.size() || result.size() != b.size())
66 {
68 << "Field sizes do not match: " << result.size() << " ("
69 << a.size() << ' ' << b.size() << ')' << nl
70 << abort(FatalError);
71 }
72 #endif
73
74 std::transform(a.cbegin(), a.cend(), b.cbegin(), result.begin(), bop);
75}
76
77
78template<class T, class BinaryOp>
80(
81 Field<T>& result,
82 const Field<T>& a,
83 const Field<T>& b,
84 const BinaryOp& bop
85)
86{
87 #ifdef FULLDEBUG
88 if (result.size() != a.size() || result.size() != b.size())
89 {
91 << "Field sizes do not match: " << result.size() << " ("
92 << a.size() << ' ' << b.size() << ')' << nl
93 << abort(FatalError);
94 }
95 #endif
96
97 forAll(result, i)
98 {
99 result[i] = bop(a[i], b[i]) ? a[i] : b[i];
100 }
101}
102
103
104template<class T, class BoolListType, class FlipOp>
106(
107 Field<T>& result,
108 const BoolListType& cond,
109 const Field<T>& a,
110 const Field<T>& b,
111 const FlipOp& flip
112)
113{
114 #ifdef FULLDEBUG
115 if (result.size() != a.size() || result.size() != b.size())
116 {
118 << "Field sizes do not match: " << result.size() << " ("
119 << a.size() << ' ' << b.size() << ')' << nl
120 << abort(FatalError);
121 }
122 #endif
123
124 forAll(result, i)
126 result[i] = flip(cond[i]) ? a[i] : b[i];
127 }
128}
129
130
131template<class T, class FlipOp>
133(
134 Field<T>& result,
135 const bitSet& cond,
136 const Field<T>& a,
137 const Field<T>& b,
138 const FlipOp& flip
139)
140{
141 #ifdef FULLDEBUG
142 if (result.size() != a.size() || result.size() != b.size())
143 {
145 << "Field sizes do not match: " << result.size() << " ("
146 << a.size() << ' ' << b.size() << ')' << nl
147 << abort(FatalError);
148 }
149 #endif
150
151 forAll(result, i)
152 {
153 result[i] = flip(cond[i]) ? a[i] : b[i];
154 }
155}
156
157
158template<class T1, class T2>
160(
161 const Field<T1>& vals,
162 const Field<T2>& data
163)
164{
165 Tuple2<T1,T2> result(pTraits<T1>::max, Zero);
166
167 const label i = findMin(vals);
168 if (i != -1)
169 {
170 result.first() = vals[i];
171 result.second() = data[i];
172 }
173
174 Pstream::combineReduce(result, minFirstEqOp<T1>());
175 return result;
176}
177
178
179template<class T1, class T2>
181(
182 const Field<T1>& vals,
183 const Field<T2>& data
184)
185{
186 Tuple2<T1,T2> result(pTraits<T1>::min, Zero);
187
188 const label i = findMax(vals);
189 if (i != -1)
190 {
191 result.first() = vals[i];
192 result.second() = data[i];
193 }
194
195 Pstream::combineReduce(result, maxFirstEqOp<T1>());
196 return result;
197}
198
199
200// ************************************************************************* //
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition Tuple2.H:51
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant UList.
Definition UListI.H:468
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
Definition UListI.H:424
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
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:28
void ternarySelect(Field< T > &result, const BoolListType &cond, const Field< T > &a, const Field< T > &b, const FlipOp &flip)
Emulate a ternary operation, selecting values from a or b depending on the conditional.
Definition FieldOps.C:99
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)
Emulate a ternary operation, selecting values from a or b depending on the binary predicate.
Definition FieldOps.C:73
label findMin(const ListType &input, label start=0)
Linear search for the index of the min element, similar to std::min_element but for lists and returns...
errorManip< error > abort(error &err)
Definition errorManip.H:139
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
label findMax(const ListType &input, label start=0)
Linear search for the index of the max element, similar to std::max_element but for lists and returns...
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
volScalarField & b
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299