Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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.
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
28template<class Type>
30(
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>("tangential" + ibin));
77 }
78 else
79 {
80 writeTabbed(os, writeComponents<Type>("patch" + ibin));
81 }
82 }
83
84 os << endl;
85}
86
87
88template<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 forAll(pts, facei)
175 {
176 const label bini = whichBin(pts[facei] & binDir_);
177
178 if (bini >= 0)
179 {
180 const Type& v = fld.boundaryField()[patchi][facei];
181
182 if (!decomposePatchValues(data, bini, v, np[facei]))
183 {
184 data[1][bini] += v;
185 }
186 }
187 }
188 }
189
190 if (Pstream::parRun())
191 {
192 for (auto& binList : data)
193 {
194 reduce(binList, sumOp<List<Type>>());
195 }
196 }
197
198 if (writeToFile())
199 {
200 writeBinnedData(data, filePtrs_[fieldi]);
201 }
202
203 return true;
204}
205
206
207// ************************************************************************* //
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
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.
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 a binned-data file.
MinMax< scalar > binLimits_
The min/max bounds for the bins.
scalar binWidth_
Distance between bin divisions.
bool processField(const label fieldi)
Apply the binning to field fieldi.
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)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
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).
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:26
Field< vector > vectorField
Specialisation of Field<T> for vector.
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
const pointField & pts
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299