AMIInterpolationTemplates.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2015-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "profiling.H"
30 #include "mapDistribute.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class Type, class CombineOp>
36 (
37  const scalar lowWeightCorrection,
38  const labelListList& allSlots,
39  const scalarListList& allWeights,
40  const scalarField& weightsSum,
41  const UList<Type>& fld,
42  const CombineOp& cop,
43  List<Type>& result,
44  const UList<Type>& defaultValues
45 )
46 {
47  if (lowWeightCorrection > 0)
48  {
49  forAll(result, facei)
50  {
51  if (weightsSum[facei] < lowWeightCorrection)
52  {
53  result[facei] = defaultValues[facei];
54  }
55  else
56  {
57  const labelList& slots = allSlots[facei];
58  const scalarList& weights = allWeights[facei];
59 
60  forAll(slots, i)
61  {
62  cop(result[facei], facei, fld[slots[i]], weights[i]);
63  }
64  }
65  }
66  }
67  else
68  {
69  forAll(result, facei)
70  {
71  const labelList& slots = allSlots[facei];
72  const scalarList& weights = allWeights[facei];
73 
74  forAll(slots, i)
75  {
76  cop(result[facei], facei, fld[slots[i]], weights[i]);
77  }
78  }
79  }
80 }
81 
82 
83 template<class Type>
85 (
86  const bool interpolateToSource,
87  const UList<Type>& fld,
88  List<Type>& result,
89  const UList<Type>& defaultValues
90 ) const
91 {
92  weightedSum
93  (
94  lowWeightCorrection_,
95  (interpolateToSource ? srcAddress_ : tgtAddress_),
96  (interpolateToSource ? srcWeights_ : tgtWeights_),
97  (interpolateToSource ? srcWeightsSum_ : tgtWeightsSum_),
98  fld,
99  multiplyWeightedOp<Type, plusEqOp<Type>>(plusEqOp<Type>()),
100  result,
101  defaultValues
102  );
103 }
104 
105 
106 template<class Type, class CombineOp>
108 (
109  const UList<Type>& fld,
110  const CombineOp& cop,
111  List<Type>& result,
112  const UList<Type>& defaultValues
113 ) const
114 {
115  addProfiling(ami, "AMIInterpolation::interpolateToTarget");
116 
117  if (fld.size() != srcAddress_.size())
118  {
120  << "Supplied field size is not equal to source patch size" << nl
121  << " source patch = " << srcAddress_.size() << nl
122  << " target patch = " << tgtAddress_.size() << nl
123  << " supplied field = " << fld.size()
124  << abort(FatalError);
125  }
126  else if
127  (
128  (lowWeightCorrection_ > 0)
129  && (defaultValues.size() != tgtAddress_.size())
130  )
131  {
133  << "Employing default values when sum of weights falls below "
134  << lowWeightCorrection_
135  << " but supplied default field size is not equal to target "
136  << "patch size" << nl
137  << " default values = " << defaultValues.size() << nl
138  << " target patch = " << tgtAddress_.size() << nl
139  << abort(FatalError);
140  }
141 
142  result.setSize(tgtAddress_.size());
143  List<Type> work;
144 
145  if (distributed())
146  {
147  const mapDistribute& map = srcMapPtr_();
148  work = fld; // deep copy
149  map.distribute(work);
150  }
151 
152  weightedSum
153  (
154  lowWeightCorrection_,
155  tgtAddress_,
156  tgtWeights_,
157  tgtWeightsSum_,
158  (distributed() ? work : fld),
159  cop,
160  result,
161  defaultValues
162  );
163 }
164 
165 
166 template<class Type, class CombineOp>
168 (
169  const UList<Type>& fld,
170  const CombineOp& cop,
171  List<Type>& result,
172  const UList<Type>& defaultValues
173 ) const
174 {
175  addProfiling(ami, "AMIInterpolation::interpolateToSource");
176 
177  if (fld.size() != tgtAddress_.size())
178  {
180  << "Supplied field size is not equal to target patch size" << nl
181  << " source patch = " << srcAddress_.size() << nl
182  << " target patch = " << tgtAddress_.size() << nl
183  << " supplied field = " << fld.size()
184  << abort(FatalError);
185  }
186  else if
187  (
188  (lowWeightCorrection_ > 0)
189  && (defaultValues.size() != srcAddress_.size())
190  )
191  {
193  << "Employing default values when sum of weights falls below "
194  << lowWeightCorrection_
195  << " but number of default values is not equal to source "
196  << "patch size" << nl
197  << " default values = " << defaultValues.size() << nl
198  << " source patch = " << srcAddress_.size() << nl
199  << abort(FatalError);
200  }
201 
202  result.setSize(srcAddress_.size());
203  List<Type> work;
204 
205  if (distributed())
206  {
207  const mapDistribute& map = tgtMapPtr_();
208  work = fld; // deep copy
209  map.distribute(work);
210  }
211 
212  weightedSum
213  (
214  lowWeightCorrection_,
215  srcAddress_,
216  srcWeights_,
217  srcWeightsSum_,
218  (distributed() ? work : fld),
219  cop,
220  result,
221  defaultValues
222  );
223 }
224 
225 
226 template<class Type, class CombineOp>
228 (
229  const Field<Type>& fld,
230  const CombineOp& cop,
231  const UList<Type>& defaultValues
232 ) const
233 {
234  auto tresult = tmp<Field<Type>>::New(srcAddress_.size(), Zero);
235 
236  interpolateToSource
237  (
238  fld,
240  tresult.ref(),
241  defaultValues
242  );
244  return tresult;
245 }
246 
247 
248 template<class Type, class CombineOp>
250 (
251  const tmp<Field<Type>>& tFld,
252  const CombineOp& cop,
253  const UList<Type>& defaultValues
254 ) const
255 {
256  return interpolateToSource(tFld(), cop, defaultValues);
257 }
258 
259 
260 template<class Type, class CombineOp>
262 (
263  const Field<Type>& fld,
264  const CombineOp& cop,
265  const UList<Type>& defaultValues
266 ) const
267 {
268  auto tresult = tmp<Field<Type>>::New(tgtAddress_.size(), Zero);
269 
270  interpolateToTarget
271  (
272  fld,
274  tresult.ref(),
275  defaultValues
276  );
278  return tresult;
279 }
280 
281 
282 template<class Type, class CombineOp>
284 (
285  const tmp<Field<Type>>& tFld,
286  const CombineOp& cop,
287  const UList<Type>& defaultValues
288 ) const
289 {
290  return interpolateToTarget(tFld(), cop, defaultValues);
291 }
292 
293 
294 template<class Type>
296 (
297  const Field<Type>& fld,
298  const UList<Type>& defaultValues
299 ) const
300 {
301  return interpolateToSource(fld, plusEqOp<Type>(), defaultValues);
302 }
303 
304 
305 template<class Type>
307 (
308  const tmp<Field<Type>>& tFld,
309  const UList<Type>& defaultValues
310 ) const
311 {
312  return interpolateToSource(tFld(), plusEqOp<Type>(), defaultValues);
313 }
314 
315 
316 template<class Type>
318 (
319  const Field<Type>& fld,
320  const UList<Type>& defaultValues
321 ) const
322 {
323  return interpolateToTarget(fld, plusEqOp<Type>(), defaultValues);
324 }
325 
326 
327 template<class Type>
329 (
330  const tmp<Field<Type>>& tFld,
331  const UList<Type>& defaultValues
332 ) const
333 {
334  return interpolateToTarget(tFld(), plusEqOp<Type>(), defaultValues);
335 }
336 
337 
338 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
#define addProfiling(Name, Descr)
Define profiling trigger with specified name and description string.
void setSize(const label n)
Alias for resize()
Definition: List.H:316
void interpolateToTarget(const UList< Type > &fld, const CombineOp &cop, List< Type > &result, const UList< Type > &defaultValues=UList< Type >::null()) const
Interpolate from source to target with supplied op to combine existing value with remote value and we...
errorManip< error > abort(error &err)
Definition: errorManip.H:139
static void weightedSum(const scalar lowWeightCorrection, const labelListList &allSlots, const scalarListList &allWeights, const scalarField &weightsSum, const UList< Type > &fld, const CombineOp &cop, List< Type > &result, const UList< Type > &defaultValues)
Weighted sum of contributions.
void interpolateToSource(const UList< Type > &fld, const CombineOp &cop, List< Type > &result, const UList< Type > &defaultValues=UList< Type >::null()) const
Interpolate from target to source with supplied op to combine existing value with remote value and we...
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
A class for managing temporary objects.
Definition: HashPtrTable.H:50
scalar lowWeightCorrection() const
Threshold weight below which interpolation is deactivated.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127