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.resize_nocopy(map.constructSize());
149  SubList<Type>(work, fld.size()) = fld; // deep copy
150  map.distribute(work);
151  }
152 
153  weightedSum
154  (
155  lowWeightCorrection_,
156  tgtAddress_,
157  tgtWeights_,
158  tgtWeightsSum_,
159  (distributed() ? work : fld),
160  cop,
161  result,
162  defaultValues
163  );
164 }
165 
166 
167 template<class Type, class CombineOp>
169 (
170  const UList<Type>& fld,
171  const CombineOp& cop,
172  List<Type>& result,
173  const UList<Type>& defaultValues
174 ) const
175 {
176  addProfiling(ami, "AMIInterpolation::interpolateToSource");
177 
178  if (fld.size() != tgtAddress_.size())
179  {
181  << "Supplied field size is not equal to target patch size" << nl
182  << " source patch = " << srcAddress_.size() << nl
183  << " target patch = " << tgtAddress_.size() << nl
184  << " supplied field = " << fld.size()
185  << abort(FatalError);
186  }
187  else if
188  (
189  (lowWeightCorrection_ > 0)
190  && (defaultValues.size() != srcAddress_.size())
191  )
192  {
194  << "Employing default values when sum of weights falls below "
195  << lowWeightCorrection_
196  << " but number of default values is not equal to source "
197  << "patch size" << nl
198  << " default values = " << defaultValues.size() << nl
199  << " source patch = " << srcAddress_.size() << nl
200  << abort(FatalError);
201  }
202 
203  result.setSize(srcAddress_.size());
204  List<Type> work;
205 
206  if (distributed())
207  {
208  const mapDistribute& map = tgtMapPtr_();
209  work.resize_nocopy(map.constructSize());
210  SubList<Type>(work, fld.size()) = fld; // deep copy
211  map.distribute(work);
212  }
213 
214  weightedSum
215  (
216  lowWeightCorrection_,
217  srcAddress_,
218  srcWeights_,
219  srcWeightsSum_,
220  (distributed() ? work : fld),
221  cop,
222  result,
223  defaultValues
224  );
225 }
226 
227 
228 template<class Type, class CombineOp>
230 (
231  const Field<Type>& fld,
232  const CombineOp& cop,
233  const UList<Type>& defaultValues
234 ) const
235 {
236  auto tresult = tmp<Field<Type>>::New(srcAddress_.size(), Zero);
237 
238  interpolateToSource
239  (
240  fld,
242  tresult.ref(),
243  defaultValues
244  );
246  return tresult;
247 }
248 
249 
250 template<class Type, class CombineOp>
252 (
253  const tmp<Field<Type>>& tFld,
254  const CombineOp& cop,
255  const UList<Type>& defaultValues
256 ) const
257 {
258  return interpolateToSource(tFld(), cop, defaultValues);
259 }
260 
261 
262 template<class Type, class CombineOp>
264 (
265  const Field<Type>& fld,
266  const CombineOp& cop,
267  const UList<Type>& defaultValues
268 ) const
269 {
270  auto tresult = tmp<Field<Type>>::New(tgtAddress_.size(), Zero);
271 
272  interpolateToTarget
273  (
274  fld,
276  tresult.ref(),
277  defaultValues
278  );
280  return tresult;
281 }
282 
283 
284 template<class Type, class CombineOp>
286 (
287  const tmp<Field<Type>>& tFld,
288  const CombineOp& cop,
289  const UList<Type>& defaultValues
290 ) const
291 {
292  return interpolateToTarget(tFld(), cop, defaultValues);
293 }
294 
295 
296 template<class Type>
298 (
299  const Field<Type>& fld,
300  const UList<Type>& defaultValues
301 ) const
302 {
303  return interpolateToSource(fld, plusEqOp<Type>(), defaultValues);
304 }
305 
306 
307 template<class Type>
309 (
310  const tmp<Field<Type>>& tFld,
311  const UList<Type>& defaultValues
312 ) const
313 {
314  return interpolateToSource(tFld(), plusEqOp<Type>(), defaultValues);
315 }
316 
317 
318 template<class Type>
320 (
321  const Field<Type>& fld,
322  const UList<Type>& defaultValues
323 ) const
324 {
325  return interpolateToTarget(fld, plusEqOp<Type>(), defaultValues);
326 }
327 
328 
329 template<class Type>
331 (
332  const tmp<Field<Type>>& tFld,
333  const UList<Type>& defaultValues
334 ) const
335 {
336  return interpolateToTarget(tFld(), plusEqOp<Type>(), defaultValues);
337 }
338 
339 
340 // ************************************************************************* //
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:608
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:421
void setSize(const label n)
Alias for resize()
Definition: List.H:320
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