singleDirectionUniformBinTemplates.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) 2021-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 template<class Type>
30 (
31  OFstream& os
32 ) const
33 {
34  writeHeaderValue(os, "bins", nBin_);
35  writeHeaderValue(os, "start", binLimits_.min());
36  writeHeaderValue(os, "end", binLimits_.max());
37  writeHeaderValue(os, "delta", binWidth_);
38  writeHeaderValue(os, "direction", binDir_);
39 
40  // Compute and print bin end points in the binning direction
41  vectorField binPoints(nBin_);
42  writeCommented(os, "x co-ords :");
43  forAll(binPoints, pointi)
44  {
45  binPoints[pointi] = (binLimits_.min() + (pointi + 1)*binWidth_)*binDir_;
46  os << tab << binPoints[pointi].x();
47  }
48  os << nl;
49 
50  writeCommented(os, "y co-ords :");
51  forAll(binPoints, pointi)
52  {
53  os << tab << binPoints[pointi].y();
54  }
55  os << nl;
56 
57  writeCommented(os, "z co-ords :");
58  forAll(binPoints, pointi)
59  {
60  os << tab << binPoints[pointi].z();
61  }
62  os << nl;
63 
64  writeHeader(os, "");
65  writeCommented(os, "Time");
66 
67  for (label i = 0; i < nBin_; ++i)
68  {
69  const word ibin("_" + Foam::name(i));
70  writeTabbed(os, writeComponents<Type>("total" + ibin));
71  writeTabbed(os, writeComponents<Type>("internal" + ibin));
72 
74  {
75  writeTabbed(os, writeComponents<Type>("normal" + ibin));
76  writeTabbed(os, writeComponents<Type>("tangenial" + ibin));
77  }
78  else
79  {
80  writeTabbed(os, writeComponents<Type>("patch" + ibin));
81  }
82  }
83 
84  os << endl;
85 }
86 
87 
88 template<class Type>
90 (
91  const label fieldi
92 )
93 {
94  const word& fieldName = fieldNames_[fieldi];
95 
97 
98  const VolFieldType* fieldPtr = mesh_.findObject<VolFieldType>(fieldName);
99 
100  if (!fieldPtr)
101  {
102  return false;
103  }
104 
105  if (writeToFile() && !writtenHeader_)
106  {
107  writeFileHeader<Type>(filePtrs_[fieldi]);
108  }
109 
110  const VolFieldType& fld = *fieldPtr;
111 
112  // Total number of fields
113  //
114  // 0: internal
115  // 1: patch total
116  //
117  // OR
118  //
119  // 0: internal
120  // 1: patch normal
121  // 2: patch tangential
122  label nField = 2;
123  if (decomposePatchValues_)
124  {
125  nField += 1;
126  }
127 
128  List<List<Type>> data(nField);
129  for (auto& binList : data)
130  {
131  binList.resize(nBin_, Zero);
132  }
133 
134  const auto whichBin = [&](const scalar d) -> label
135  {
136  if (d >= binLimits_.min() && d <= binLimits_.max())
137  {
138  // Find the bin division
139  label bini = floor
140  (
141  (d - binLimits_.min())/binWidth_
142  );
143  return min(max(bini, 0), nBin_ - 1);
144  }
145  else
146  {
147  return -1;
148  }
149  };
150 
151 
152  for (const label zonei : cellZoneIDs_)
153  {
154  const cellZone& cZone = mesh_.cellZones()[zonei];
155 
156  for (const label celli : cZone)
157  {
158  const label bini = whichBin(mesh_.C()[celli] & binDir_);
159 
160  if (bini >= 0)
161  {
162  data[0][bini] += fld[celli];
163  }
164  }
165  }
166 
167  for (const label patchi : patchIDs_)
168  {
169  const polyPatch& pp = mesh_.boundaryMesh()[patchi];
170  const vectorField np(mesh_.boundary()[patchi].nf());
171 
172  const auto& pts = pp.faceCentres();
173 
174  const scalarField dd(pp.faceCentres() & binDir_);
175 
176  forAll(pts, facei)
177  {
178  const label bini = whichBin(pts[facei] & binDir_);
179 
180  if (bini >= 0)
181  {
182  const Type& v = fld.boundaryField()[patchi][facei];
183 
184  if (!decomposePatchValues(data, bini, v, np[facei]))
185  {
186  data[1][bini] += v;
187  }
188  }
189  }
190  }
191 
192  for (auto& binList : data)
193  {
194  reduce(binList, sumOp<List<Type>>());
195  }
196 
197  if (writeToFile())
198  {
199  writeBinnedData(data, filePtrs_[fieldi]);
200  }
201 
202  return true;
203 }
204 
205 
206 // ************************************************************************* //
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:339
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
Output to file stream, using an OSstream.
Definition: OFstream.H:49
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
bool decomposePatchValues_
Decompose patch values into normal and tangential components.
Definition: binModel.H:73
label nBin_
Total number of bins.
Definition: binModel.H:89
constexpr char tab
The tab &#39;\t&#39; character(0x09)
Definition: Ostream.H:49
Generic GeometricField class.
Definition: areaFieldsFwd.H:50
bool processField(const label fieldi)
Apply the binning to field fieldi.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:313
const T & min() const noexcept
The min value (first)
Definition: MinMaxI.H:107
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
OBJstream os(runTime.globalPath()/outputName)
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))
scalar binWidth_
Distance between bin divisions.
void writeFileHeader(OFstream &os) const
Write header for a binned-data file.
const T & max() const noexcept
The max value (second)
Definition: MinMaxI.H:121
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Reduce inplace (cf. MPI Allreduce) using specified communication schedule.
Field< vector > vectorField
Specialisation of Field<T> for vector.
MinMax< scalar > binLimits_
The min/max bounds for the bins.
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())
const pointField & pts
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:329