Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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
30template<class Type>
32(
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
87template<class Type>
88bool 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, List<Type>(nBin_, Zero));
125
126 for (const label zonei : cellZoneIDs_)
127 {
128 const cellZone& cZone = mesh_.cellZones()[zonei];
129
130 for (const label celli : cZone)
131 {
132 const label bini = cellToBin_[celli];
133
134 if (bini >= 0)
135 {
136 data[0][bini] += fld[celli];
137 }
138 }
139 }
140
141 for (const label patchi : patchIDs_)
142 {
143 const polyPatch& pp = mesh_.boundaryMesh()[patchi];
144 const vectorField np(mesh_.boundary()[patchi].nf());
145
146 forAll(pp, facei)
147 {
148 const label localFacei =
149 pp.start() - mesh_.nInternalFaces() + facei;
150 const label bini = faceToBin_[localFacei];
151
152 if (bini >= 0)
153 {
154 const Type& v = fld.boundaryField()[patchi][facei];
155
156 if (!decomposePatchValues(data, bini, v, np[facei]))
157 {
158 data[1][bini] += v;
159 }
160 }
161 }
162 }
163
164 if (Pstream::parRun())
165 {
166 for (auto& binList : data)
167 {
168 reduce(binList, sumOp<List<Type>>());
169 }
170 }
171
172 if (writeToFile())
173 {
174 writeBinnedData(data, filePtrs_[fieldi]);
175 }
176
177 return true;
178}
179
180
181// ************************************************************************* //
#define R(A, B, C, D, E, F, K, M)
Info<< nl;Info<< "Write faMesh in vtk format:"<< nl;{ vtk::uindirectPatchWriter writer(aMesh.patch(), fileName(aMesh.time().globalPath()/vtkBaseFileName));writer.writeGeometry();globalIndex procAddr(aMesh.nFaces());labelList cellIDs;if(UPstream::master()) { cellIDs.resize(procAddr.totalSize());for(const labelRange &range :procAddr.ranges()) { auto slice=cellIDs.slice(range);slice=identity(range);} } writer.beginCellData(4);writer.writeProcIDs();writer.write("cellID", cellIDs);writer.write("area", aMesh.S().field());writer.write("normal", aMesh.faceAreaNormals());writer.beginPointData(1);writer.write("normal", aMesh.pointAreaNormals());Info<< " "<< writer.output().name()<< nl;}{ vtk::lineWriter writer(aMesh.points(), aMesh.edges(), fileName(aMesh.time().globalPath()/(vtkBaseFileName+"-edges")));writer.writeGeometry();writer.beginCellData(4);writer.writeProcIDs();{ Field< scalar > fld(faMeshTools::flattenEdgeField(aMesh.magLe(), true))
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())
Generic GeometricField class.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
static bool parRun(const bool on) noexcept
Set as parallel run on/off.
Definition UPstream.H:1669
static bool & parRun() noexcept
Test if this a parallel run.
Definition UPstream.H:1681
static constexpr direction nComponents
Number of components in this vector space.
string writeComponents(const word &stem) const
Helper function to construct a string description for a given type.
const fvMesh & mesh_
Reference to the mesh.
Definition binModel.H:68
PtrList< OFstream > filePtrs_
List of file pointers; 1 file per field.
Definition binModel.H:109
void writeBinnedData(List< List< Type > > &data, Ostream &os) const
Write binned data to stream.
wordList fieldNames_
Names of operand fields.
Definition binModel.H:99
label nBin_
Total number of bins.
Definition binModel.H:89
bool decomposePatchValues(List< List< Type > > &data, const label bini, const Type &v, const vector &n) const
Helper function to decompose patch values into normal and tangential components.
autoPtr< coordinateSystem > coordSysPtr_
Local coordinate system of bins.
Definition binModel.H:84
bool decomposePatchValues_
Decompose patch values into normal and tangential components.
Definition binModel.H:73
labelList cellZoneIDs_
Indices of operand cell zones.
Definition binModel.H:104
labelList patchIDs_
Indices of operand patches.
Definition binModel.H:94
void writeFileHeader(OFstream &os) const
Write header for an binned-data file.
MinMax< vector > binLimits_
The geometric min/max bounds for the bins.
Definition uniformBin.H:178
vector binWidth_
Equidistant bin widths in binning directions.
Definition uniformBin.H:173
labelList cellToBin_
Cell index to bin index addressing.
Definition uniformBin.H:188
bool processField(const label fieldi)
Apply the binning to field fieldi.
labelList faceToBin_
Face index to bin index addressing.
Definition uniformBin.H:183
Vector< label > nBins_
Numbers of bins in binning directions.
Definition uniformBin.H:168
A subset of mesh cells.
Definition cellZone.H:61
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition writeFile.C:334
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition writeFile.C:344
bool writtenHeader_
Flag to identify whether the header has been written.
Definition writeFile.H:157
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition writeFile.C:318
virtual bool writeToFile() const
Flag to allow writing to file.
Definition writeFile.C:286
A patch is a list of labels that address the faces in the global face list.
Definition polyPatch.H:73
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
Tensor< scalar > tensor
Definition symmTensor.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
void reduce(T &value, BinaryOp bop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Reduce inplace (cf. MPI Allreduce).
Field< vector > vectorField
Specialisation of Field<T> for vector.
uint8_t direction
Definition direction.H:49
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
constexpr char tab
The tab '\t' character(0x09).
Definition Ostream.H:49
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299