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 
29 template<class Type>
31 (
32  OFstream& os
33 ) const
34 {
35  writeHeaderValue(os, "bins", nBin_);
36  writeHeaderValue(os, "start", binMin_);
37  writeHeaderValue(os, "end", binMax_);
38  writeHeaderValue(os, "delta", binDx_);
39  writeHeaderValue(os, "direction", binDir_);
40 
41  // Compute and print bin end points in the binning direction
42  vectorField binPoints(nBin_);
43  writeCommented(os, "x co-ords :");
44  forAll(binPoints, pointi)
45  {
46  binPoints[pointi] = (binMin_ + (pointi + 1)*binDx_)*binDir_;
47  os << tab << binPoints[pointi].x();
48  }
49  os << nl;
50 
51  writeCommented(os, "y co-ords :");
52  forAll(binPoints, pointi)
53  {
54  os << tab << binPoints[pointi].y();
55  }
56  os << nl;
57 
58  writeCommented(os, "z co-ords :");
59  forAll(binPoints, pointi)
60  {
61  os << tab << binPoints[pointi].z();
62  }
63  os << nl;
64 
65  writeHeader(os, "");
66  writeCommented(os, "Time");
67 
68  for (label i = 0; i < nBin_; ++i)
69  {
70  const word ibin("_" + Foam::name(i));
71  writeTabbed(os, writeComponents<Type>("total" + ibin));
72  writeTabbed(os, writeComponents<Type>("internal" + ibin));
73 
75  {
76  writeTabbed(os, writeComponents<Type>("normal" + ibin));
77  writeTabbed(os, writeComponents<Type>("tangenial" + ibin));
78  }
79  else
80  {
81  writeTabbed(os, writeComponents<Type>("patch" + ibin));
82  }
83 
84  }
85 
86  os << endl;
87 }
88 
89 
90 template<class Type>
92 (
93  const label fieldi
94 )
95 {
96  const word& fieldName = fieldNames_[fieldi];
97 
99 
100  const VolFieldType* fieldPtr = mesh_.findObject<VolFieldType>(fieldName);
101 
102  if (!fieldPtr)
103  {
104  return false;
105  }
106 
107  if (writeToFile() && !writtenHeader_)
108  {
109  writeFileHeader<Type>(filePtrs_[fieldi]);
110  }
111 
112  const VolFieldType& fld = *fieldPtr;
113 
114  // Total number of fields
115  //
116  // 0: internal
117  // 1: patch total
118  //
119  // OR
120  //
121  // 0: internal
122  // 1: patch normal
123  // 2: patch tangential
124  label nField = 2;
125  if (decomposePatchValues_)
126  {
127  nField += 1;
128  }
129 
130  List<List<Type>> data(nField);
131  for (auto& binList : data)
132  {
133  binList.setSize(nBin_, Zero);
134  }
135 
136  for (const label zonei : cellZoneIDs_)
137  {
138  const cellZone& cZone = mesh_.cellZones()[zonei];
139 
140  for (const label celli : cZone)
141  {
142  const scalar dd = mesh_.C()[celli] & binDir_;
143 
144  if (dd < binMin_ || dd > binMax_)
145  {
146  continue;
147  }
148 
149  // Find the bin division corresponding to the cell
150  const label bini =
151  min(max(floor((dd - binMin_)/binDx_), 0), nBin_ - 1);
152 
153  data[0][bini] += fld[celli];
154  }
155  }
156 
157  forAllIters(patchSet_, iter)
158  {
159  const label patchi = iter();
160  const polyPatch& pp = mesh_.boundaryMesh()[patchi];
161  const vectorField np(mesh_.boundary()[patchi].nf());
162 
163  const scalarField dd(pp.faceCentres() & binDir_);
164 
165  forAll(dd, facei)
166  {
167  // Avoid faces outside of the bin
168  if (dd[facei] < binMin_ || dd[facei] > binMax_)
169  {
170  continue;
171  }
172 
173  // Find the bin division corresponding to the face
174  const label bini =
175  min(max(floor((dd[facei] - binMin_)/binDx_), 0), nBin_ - 1);
176 
177  const Type& v = fld.boundaryField()[patchi][facei];
178 
179  if (!decomposePatchValues(data, bini, v, np[facei]))
180  {
181  data[1][bini] += v;
182  }
183  }
184  }
185 
186  for (auto& binList : data)
187  {
188  reduce(binList, sumOp<List<Type>>());
189  }
190 
191  if (writeToFile())
192  {
193  writeBinnedData(data, filePtrs_[fieldi]);
194  }
195 
196  return true;
197 }
198 
199 
200 // ************************************************************************* //
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:49
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
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:48
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:413
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:313
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
A class for handling words, derived from Foam::string.
Definition: word.H:63
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:328
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))
void writeFileHeader(OFstream &os) const
Write header for a binned-data file.
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.
scalar binDx_
Distance between bin divisions.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:157
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:329