MappedFileFilterFieldTemplates.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) 2022 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 
28 #include "indexedOctree.H"
29 #include "treeDataPoint.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
33 template<class Type>
36 (
37  const tmp<Field<Type>>& tinput,
38  const label nSweeps
39 ) const
40 {
41  if (nSweeps < 1 || !tinput.good())
42  {
43  // Nothing to do
44  return tinput;
45  }
46 
47  label nPoints = tinput().size();
48 
49  const label nAddr = addressing_.size();
50 
51  if (!nPoints || !nAddr)
52  {
53  // No input or using an identity mapping
54  return tinput;
55  }
56 
57  // Output
58  auto toutput = tmp<Field<Type>>::New(nPoints);
59 
60  if (nAddr < nPoints)
61  {
63  << "Addressing/weights shorter than input field"
64  << endl;
65 
66  // Restrict addressing space within loop
67  nPoints = nAddr;
68 
69  // Fill trailing portion with the input values
70  toutput.ref().slice(nAddr) = tinput().slice(nAddr);
71  }
72 
73  // Intermediate buffer
74  tmp<Field<Type>> tbuffer;
75 
76  if (nSweeps == 1)
77  {
78  // Simple reference ie enough
79  tbuffer.cref(tinput);
80  }
81  else
82  {
83  // Need buffer for swap - copy or steal contents
84  tbuffer.reset(tinput.ptr());
85  }
86  tinput.clear();
87 
88  // If there are any 'senseless' sweeps
89  // (ie, with identity mapping of values), they will have
90  // been detected during addressing construction, can ignore here
91 
92  for (label sweep = 0; sweep < nSweeps; ++sweep)
93  {
94  if (sweep > 0)
95  {
96  // Reuse previous output for subsequent sweeps
97  tbuffer.swap(toutput);
98  }
99 
100  const auto& input = tbuffer();
101  auto& output = toutput.ref();
102 
103  #pragma omp parallel for if (nPoints > 1000)
104  for (label pointi = 0; pointi < nPoints; ++pointi)
105  {
106  const auto& addr = addressing_[pointi];
107  const auto& weight = weights_[pointi];
108 
109  auto& interp = output[pointi];
110 
111  if (addr.empty())
112  {
113  // Could happen if the search radius was really small
114  interp = input[pointi];
115  }
116  else
117  {
118  interp = Zero;
119 
120  forAll(addr, i)
121  {
122  interp += (weight[i] * input[addr[i]]);
123  }
124  }
125  }
126  }
127 
128  return toutput;
129 }
130 
131 
132 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition: tmpI.H:221
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
void sweep(volScalarField &field, const volScalarField &alpha, const label nLayers, const scalar alphaDiff=0.2)
Definition: fvcSmooth.C:224
Generic templated field type.
Definition: Field.H:62
label nPoints
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
void swap(tmp< T > &other) noexcept
Swaps the managed object with other.
Definition: tmpI.H:418
tmp< Field< Type > > evaluate(const tmp< Field< Type >> &tinput, const label nSweeps) const
Return the median smoothed field.
#define WarningInFunction
Report a warning using Foam::Warning.
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
A class for managing temporary objects.
Definition: HashPtrTable.H:50
void reset(tmp< T > &&other) noexcept
Clear existing and transfer ownership.
Definition: tmpI.H:338
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127