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 {
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() && srcMapPtr_)
146  {
147  const mapDistribute& map = srcMapPtr_();
148 
149  if (map.comm() == -1)
150  {
151  return;
152  }
153 
154  work.resize_nocopy(map.constructSize());
155  SubList<Type>(work, fld.size()) = fld; // deep copy
156  map.distribute(work);
157  }
158 
160  (
161  lowWeightCorrection_,
162  tgtAddress_,
163  tgtWeights_,
164  tgtWeightsSum_,
165  (distributed() ? work : fld),
166  cop,
167  result,
168  defaultValues
169  );
170 }
171 
172 
173 template<class Type, class CombineOp>
175 (
176  const UList<Type>& fld,
177  const CombineOp& cop,
178  List<Type>& result,
179  const UList<Type>& defaultValues
180 ) const
181 {
182  addProfiling(ami, "AMIInterpolation::interpolateToSource");
183 
184  if (fld.size() != tgtAddress_.size())
185  {
187  << "Supplied field size is not equal to target patch size" << nl
188  << " source patch = " << srcAddress_.size() << nl
189  << " target patch = " << tgtAddress_.size() << nl
190  << " supplied field = " << fld.size()
191  << abort(FatalError);
192  }
193  else if
194  (
195  (lowWeightCorrection_ > 0)
196  && (defaultValues.size() != srcAddress_.size())
197  )
198  {
200  << "Employing default values when sum of weights falls below "
201  << lowWeightCorrection_
202  << " but number of default values is not equal to source "
203  << "patch size" << nl
204  << " default values = " << defaultValues.size() << nl
205  << " source patch = " << srcAddress_.size() << nl
206  << abort(FatalError);
207  }
208 
209  result.setSize(srcAddress_.size());
210  List<Type> work;
211 
212  if (distributed() && tgtMapPtr_)
213  {
214  const mapDistribute& map = tgtMapPtr_();
215 
216  if (map.comm() == -1)
217  {
218  return;
219  }
220 
221  work.resize_nocopy(map.constructSize());
222  SubList<Type>(work, fld.size()) = fld; // deep copy
223  map.distribute(work);
224  }
225 
227  (
228  lowWeightCorrection_,
229  srcAddress_,
230  srcWeights_,
231  srcWeightsSum_,
232  (distributed() ? work : fld),
233  cop,
234  result,
235  defaultValues
236  );
237 }
238 
239 
240 template<class Type, class CombineOp>
242 (
243  const Field<Type>& fld,
244  const CombineOp& cop,
245  const UList<Type>& defaultValues
246 ) const
247 {
248  auto tresult = tmp<Field<Type>>::New(srcAddress_.size(), Zero);
249 
250  interpolateToSource
251  (
252  fld,
254  tresult.ref(),
255  defaultValues
256  );
258  return tresult;
259 }
260 
261 
262 template<class Type, class CombineOp>
264 (
265  const tmp<Field<Type>>& tFld,
266  const CombineOp& cop,
267  const UList<Type>& defaultValues
268 ) const
269 {
270  return interpolateToSource(tFld(), cop, defaultValues);
271 }
272 
273 
274 template<class Type, class CombineOp>
276 (
277  const Field<Type>& fld,
278  const CombineOp& cop,
279  const UList<Type>& defaultValues
280 ) const
281 {
282  auto tresult = tmp<Field<Type>>::New(tgtAddress_.size(), Zero);
283 
284  interpolateToTarget
285  (
286  fld,
288  tresult.ref(),
289  defaultValues
290  );
292  return tresult;
293 }
294 
295 
296 template<class Type, class CombineOp>
298 (
299  const tmp<Field<Type>>& tFld,
300  const CombineOp& cop,
301  const UList<Type>& defaultValues
302 ) const
303 {
304  return interpolateToTarget(tFld(), cop, defaultValues);
305 }
306 
307 
308 template<class Type>
310 (
311  const Field<Type>& fld,
312  const UList<Type>& defaultValues
313 ) const
314 {
315  return interpolateToSource(fld, plusEqOp<Type>(), defaultValues);
316 }
317 
318 
319 template<class Type>
321 (
322  const tmp<Field<Type>>& tFld,
323  const UList<Type>& defaultValues
324 ) const
325 {
326  return interpolateToSource(tFld(), plusEqOp<Type>(), defaultValues);
327 }
328 
329 
330 template<class Type>
332 (
333  const Field<Type>& fld,
334  const UList<Type>& defaultValues
335 ) const
336 {
337  return interpolateToTarget(fld, plusEqOp<Type>(), defaultValues);
338 }
339 
340 
341 template<class Type>
343 (
344  const tmp<Field<Type>>& tFld,
345  const UList<Type>& defaultValues
346 ) const
347 {
348  return interpolateToTarget(tFld(), plusEqOp<Type>(), defaultValues);
349 }
350 
351 
352 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Type weightedSum(const UList< scalar > &weights, const UList< Type > &fld)
The local weighted sum (integral) of a field, using the mag() of the weights.
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:600
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 addProfiling(Name,...)
Define profiling trigger with specified name and description string. The description is generated by ...
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:286
void setSize(const label n)
Alias for resize()
Definition: List.H:325
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