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 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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 
29 #include <algorithm>
30 
31 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
32 
33 template<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 
55 template<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 
78 template<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 
104 template<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)
125  {
126  result[i] = flip(cond[i]) ? a[i] : b[i];
127  }
128 }
129 
130 
131 template<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 
158 template<class T1, class T2>
160 (
161  const Field<T1>& vals,
162  const Field<T2>& data
163 )
164 {
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  Foam::combineReduce(result, minFirstEqOp<T1>());
175  return result;
176 }
177 
178 
179 template<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  Foam::combineReduce(result, maxFirstEqOp<T1>());
196  return result;
197 }
198 
199 
200 // ************************************************************************* //
Foam::FieldOps::ternary
void ternary(Field< T > &result, const Field< T > &a, const Field< T > &b, const BinaryOp &bop)
Definition: FieldOps.C:80
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::combineReduce
void combineReduce(const List< UPstream::commsStruct > &comms, T &Value, const CombineOp &cop, const int tag, const label comm)
Definition: PstreamCombineReduceOps.H:54
Foam::FieldOps::ternarySelect
void ternarySelect(Field< T > &result, const BoolListType &cond, const Field< T > &a, const Field< T > &b, const FlipOp &flip)
Definition: FieldOps.C:106
Foam::transform
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:521
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
PstreamCombineReduceOps.H
Combination-Reduction operation for a parallel run. The information from all nodes is collected on th...
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::FieldOps::findMinData
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.
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::findMax
label findMax(const ListType &input, label start=0)
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::FieldOps::assign
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:35
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:60
Foam::findMin
label findMin(const ListType &input, label start=0)
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::FieldOps::findMaxData
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.