uniformBinTemplates.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 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
29 
30 template<class Type>
32 (
33  OFstream& os
34 ) const
35 {
36  writeHeader(os, "bins");
37 
38  const tensor& R = coordSysPtr_->R();
39  for (direction i = 0; i < vector::nComponents; ++i)
40  {
41  writeHeaderValue(os, "e" + Foam::name(i) + " bins", nBins_[i]);
42  writeHeaderValue(os, " start", binLimits_.min()[i]);
43  writeHeaderValue(os, " end", binLimits_.max()[i]);
44  writeHeaderValue(os, " delta", binWidth_[i]);
45  writeHeaderValue(os, " direction", R.col(i));
46  }
47  writeCommented(os, "bin end co-ordinates:");
48  os << nl;
49 
50  // Compute and print bin end points in binning directions
51  for (direction i = 0; i < vector::nComponents; ++i)
52  {
53  scalar binEnd = binLimits_.min()[i];
54 
55  writeCommented(os, "e"+Foam::name(i)+" co-ords :");
56  for (label j = 0; j < nBins_[i]; ++j)
57  {
58  binEnd += binWidth_[i];
59  os << tab << binEnd;
60  }
61  os << nl;
62  }
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>("tangential" + ibin));
77  }
78  else
79  {
80  writeTabbed(os, writeComponents<Type>("patch" + ibin));
81  }
82  }
83 
84  os << endl;
85 }
86 
87 template<class Type>
88 bool Foam::binModels::uniformBin::processField(const label fieldi)
89 {
90  const word& fieldName = fieldNames_[fieldi];
91 
92  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
93 
94  const VolFieldType* fieldPtr = mesh_.findObject<VolFieldType>(fieldName);
95 
96  if (!fieldPtr)
97  {
98  return false;
99  }
100 
101  if (writeToFile() && !writtenHeader_)
102  {
103  writeFileHeader<Type>(filePtrs_[fieldi]);
104  }
105 
106  const VolFieldType& fld = *fieldPtr;
107 
108  // Total number of fields
109  //
110  // 0: internal
111  // 1: patch total
112  //
113  // OR
114  //
115  // 0: internal
116  // 1: patch normal
117  // 2: patch tangential
118  label nField = 2;
119  if (decomposePatchValues_)
120  {
121  nField += 1;
122  }
123 
124  List<List<Type>> data(nField);
125  for (auto& binList : data)
126  {
127  binList.resize(nBin_, Zero);
128  }
129 
130  for (const label zonei : cellZoneIDs_)
131  {
132  const cellZone& cZone = mesh_.cellZones()[zonei];
133 
134  for (const label celli : cZone)
135  {
136  const label bini = cellToBin_[celli];
137 
138  if (bini >= 0)
139  {
140  data[0][bini] += fld[celli];
141  }
142  }
143  }
144 
145  for (const label patchi : patchIDs_)
146  {
147  const polyPatch& pp = mesh_.boundaryMesh()[patchi];
148  const vectorField np(mesh_.boundary()[patchi].nf());
149 
150  forAll(pp, facei)
151  {
152  const label localFacei =
153  pp.start() - mesh_.nInternalFaces() + facei;
154  const label bini = faceToBin_[localFacei];
155 
156  if (bini >= 0)
157  {
158  const Type& v = fld.boundaryField()[patchi][facei];
159 
160  if (!decomposePatchValues(data, bini, v, np[facei]))
161  {
162  data[1][bini] += v;
163  }
164  }
165  }
166  }
167 
168  for (auto& binList : data)
169  {
170  reduce(binList, sumOp<List<Type>>());
171  }
172 
173  if (writeToFile())
174  {
175  writeBinnedData(data, filePtrs_[fieldi]);
176  }
177 
178  return true;
179 }
180 
181 
182 // ************************************************************************* //
uint8_t direction
Definition: direction.H:46
MinMax< vector > binLimits_
The geometric min/max bounds for the bins.
Definition: uniformBin.H:178
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:339
bool processField(const label fieldi)
Apply the binning to field fieldi.
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
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
#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
void writeFileHeader(OFstream &os) const
Write header for an binned-data file.
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
autoPtr< coordinateSystem > coordSysPtr_
Local coordinate system of bins.
Definition: binModel.H:84
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
OBJstream os(runTime.globalPath()/outputName)
vector binWidth_
Equidistant bin widths in binning directions.
Definition: uniformBin.H:173
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))
#define R(A, B, C, D, E, F, K, M)
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.
Tensor of scalars, i.e. Tensor<scalar>.
Vector< label > nBins_
Numbers of bins in binning directions.
Definition: uniformBin.H:168
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())
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