FacePostProcessing.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "FacePostProcessing.H"
27 #include "Pstream.H"
28 #include "ListListOps.H"
29 #include "surfaceWriter.H"
30 #include "globalIndex.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class CloudType>
36 (
37  const word& zoneName,
38  const label zoneI,
39  const label nFaces,
40  const scalar totArea
41 )
42 {
43  // Create the output file if not already created
44  if (log_)
45  {
46  if (debug)
47  {
48  Info<< "Creating output file." << endl;
49  }
50 
51  if (Pstream::master())
52  {
53  // Create directory if does not exist
54  mkDir(this->writeTimeDir());
55 
56  // Open new file at start up
57  outputFilePtr_.set
58  (
59  zoneI,
60  new OFstream
61  (
62  this->writeTimeDir()/(type() + '_' + zoneName + ".dat")
63  )
64  );
65 
66  outputFilePtr_[zoneI]
67  << "# Source : " << type() << nl
68  << "# Face zone : " << zoneName << nl
69  << "# Faces : " << nFaces << nl
70  << "# Area : " << totArea << nl
71  << "# Time" << tab << "mass" << tab << "massFlowRate" << endl;
72  }
73  }
74 }
75 
76 
77 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
78 
79 template<class CloudType>
81 {
82  const fvMesh& mesh = this->owner().mesh();
83  const Time& time = mesh.time();
84  const faceZoneMesh& fzm = mesh.faceZones();
85  scalar timeNew = time.value();
86  scalar timeElapsed = timeNew - timeOld_;
87 
88  totalTime_ += timeElapsed;
89 
90  const scalar alpha = (totalTime_ - timeElapsed)/totalTime_;
91  const scalar beta = timeElapsed/totalTime_;
92 
93  forAll(faceZoneIDs_, zoneI)
94  {
95  massFlowRate_[zoneI] =
96  alpha*massFlowRate_[zoneI] + beta*mass_[zoneI]/timeElapsed;
97  massTotal_[zoneI] += mass_[zoneI];
98  }
99 
100  const label proci = Pstream::myProcNo();
101 
102  Info<< type() << " output:" << nl;
103 
104  List<scalarField> zoneMassTotal(mass_.size());
105  List<scalarField> zoneMassFlowRate(massFlowRate_.size());
106  forAll(faceZoneIDs_, zoneI)
107  {
108  const word& zoneName = fzm[faceZoneIDs_[zoneI]].name();
109 
110  scalarListList allProcMass(Pstream::nProcs());
111  allProcMass[proci] = massTotal_[zoneI];
112  Pstream::gatherList(allProcMass);
113  zoneMassTotal[zoneI] =
114  ListListOps::combine<scalarList>
115  (
116  allProcMass, accessOp<scalarList>()
117  );
118  const scalar sumMassTotal = sum(zoneMassTotal[zoneI]);
119 
120  scalarListList allProcMassFlowRate(Pstream::nProcs());
121  allProcMassFlowRate[proci] = massFlowRate_[zoneI];
122  Pstream::gatherList(allProcMassFlowRate);
123  zoneMassFlowRate[zoneI] =
124  ListListOps::combine<scalarList>
125  (
126  allProcMassFlowRate, accessOp<scalarList>()
127  );
128  const scalar sumMassFlowRate = sum(zoneMassFlowRate[zoneI]);
129 
130  Info<< " " << zoneName
131  << ": total mass = " << sumMassTotal
132  << "; average mass flow rate = " << sumMassFlowRate
133  << nl;
134 
135  if (outputFilePtr_.set(zoneI))
136  {
137  OFstream& os = outputFilePtr_[zoneI];
138  os << time.timeName() << token::TAB << sumMassTotal << token::TAB
139  << sumMassFlowRate<< endl;
140  }
141  }
142 
143  Info<< endl;
144 
145 
146  if (surfaceFormat_ != "none")
147  {
148  forAll(faceZoneIDs_, zoneI)
149  {
150  const faceZone& fZone = fzm[faceZoneIDs_[zoneI]];
151 
152  labelList pointToGlobal;
153  labelList uniqueMeshPointLabels;
154  autoPtr<globalIndex> globalPointsPtr =
155  mesh.globalData().mergePoints
156  (
157  fZone().meshPoints(),
158  fZone().meshPointMap(),
159  pointToGlobal,
160  uniqueMeshPointLabels
161  );
162 
163  pointField uniquePoints(mesh.points(), uniqueMeshPointLabels);
164  List<pointField> allProcPoints(Pstream::nProcs());
165  allProcPoints[proci] = uniquePoints;
166  Pstream::gatherList(allProcPoints);
167 
168  faceList faces(fZone().localFaces());
169  forAll(faces, i)
170  {
171  inplaceRenumber(pointToGlobal, faces[i]);
172  }
173  List<faceList> allProcFaces(Pstream::nProcs());
174  allProcFaces[proci] = faces;
175  Pstream::gatherList(allProcFaces);
176 
177  if (Pstream::master())
178  {
180  (
181  ListListOps::combine<pointField>
182  (
183  allProcPoints, accessOp<pointField>()
184  )
185  );
186 
187  faceList allFaces
188  (
189  ListListOps::combine<faceList>
190  (
191  allProcFaces, accessOp<faceList>()
192  )
193  );
194 
196  (
198  (
199  surfaceFormat_,
200  this->coeffDict().subOrEmptyDict("formatOptions").
201  subOrEmptyDict(surfaceFormat_)
202  )
203  );
204 
205  writer->write
206  (
207  this->writeTimeDir(),
208  fZone.name(),
209  allPoints,
210  allFaces,
211  "massTotal",
212  zoneMassTotal[zoneI],
213  false
214  );
215 
216  writer->write
217  (
218  this->writeTimeDir(),
219  fZone.name(),
220  allPoints,
221  allFaces,
222  "massFlowRate",
223  zoneMassFlowRate[zoneI],
224  false
225  );
226  }
227  }
228  }
229 
230 
231  if (resetOnWrite_)
232  {
233  forAll(faceZoneIDs_, zoneI)
234  {
235  massFlowRate_[zoneI] = 0.0;
236  }
237  timeOld_ = timeNew;
238  totalTime_ = 0.0;
239  }
240 
241  forAll(mass_, zoneI)
242  {
243  mass_[zoneI] = 0.0;
244  }
245 
246  // writeProperties();
247 }
248 
249 
250 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
251 
252 template<class CloudType>
254 (
255  const dictionary& dict,
256  CloudType& owner,
257  const word& modelName
258 )
259 :
260  CloudFunctionObject<CloudType>(dict, owner, modelName, typeName),
261  faceZoneIDs_(),
262  surfaceFormat_(this->coeffDict().lookup("surfaceFormat")),
263  resetOnWrite_(this->coeffDict().lookup("resetOnWrite")),
264  totalTime_(0.0),
265  mass_(),
266  massTotal_(),
267  massFlowRate_(),
268  log_(this->coeffDict().lookup("log")),
269  outputFilePtr_(),
270  timeOld_(owner.mesh().time().value())
271 {
272  wordList faceZoneNames(this->coeffDict().lookup("faceZones"));
273  mass_.setSize(faceZoneNames.size());
274  massTotal_.setSize(faceZoneNames.size());
275  massFlowRate_.setSize(faceZoneNames.size());
276 
277  outputFilePtr_.setSize(faceZoneNames.size());
278 
279  DynamicList<label> zoneIDs;
280  const faceZoneMesh& fzm = owner.mesh().faceZones();
281  const surfaceScalarField& magSf = owner.mesh().magSf();
282  const polyBoundaryMesh& pbm = owner.mesh().boundaryMesh();
283  forAll(faceZoneNames, i)
284  {
285  const word& zoneName = faceZoneNames[i];
286  label zoneI = fzm.findZoneID(zoneName);
287  if (zoneI != -1)
288  {
289  zoneIDs.append(zoneI);
290  const faceZone& fz = fzm[zoneI];
291  mass_[i].setSize(fz.size(), 0.0);
292  massTotal_[i].setSize(fz.size(), 0.0);
293  massFlowRate_[i].setSize(fz.size(), 0.0);
294 
295  label nFaces = returnReduce(fz.size(), sumOp<label>());
296  Info<< " " << zoneName << " faces: " << nFaces << nl;
297 
298  scalar totArea = 0.0;
299  forAll(fz, j)
300  {
301  label facei = fz[j];
302  if (facei < owner.mesh().nInternalFaces())
303  {
304  totArea += magSf[fz[j]];
305  }
306  else
307  {
308  label bFacei = facei - owner.mesh().nInternalFaces();
309  label patchi = pbm.patchID()[bFacei];
310  const polyPatch& pp = pbm[patchi];
311 
312  if
313  (
314  !magSf.boundaryField()[patchi].coupled()
315  || refCast<const coupledPolyPatch>(pp).owner()
316  )
317  {
318  label localFacei = pp.whichFace(facei);
319  totArea += magSf.boundaryField()[patchi][localFacei];
320  }
321  }
322  }
323  totArea = returnReduce(totArea, sumOp<scalar>());
324 
325  makeLogFile(zoneName, i, nFaces, totArea);
326  }
327  }
328 
329  faceZoneIDs_.transfer(zoneIDs);
330 
331  // readProperties(); AND initialise mass... fields
332 }
333 
334 
335 template<class CloudType>
337 (
339 )
340 :
342  faceZoneIDs_(pff.faceZoneIDs_),
343  surfaceFormat_(pff.surfaceFormat_),
344  resetOnWrite_(pff.resetOnWrite_),
345  totalTime_(pff.totalTime_),
346  mass_(pff.mass_),
347  massTotal_(pff.massTotal_),
348  massFlowRate_(pff.massFlowRate_),
349  log_(pff.log_),
350  outputFilePtr_(),
351  timeOld_(0.0)
352 {}
353 
354 
355 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
356 
357 template<class CloudType>
359 {}
360 
361 
362 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
363 
364 template<class CloudType>
365 void Foam::FacePostProcessing<CloudType>::postFace(const parcelType& p, bool&)
366 {
367  if
368  (
369  this->owner().solution().output()
370  || this->owner().solution().transient()
371  )
372  {
373  const faceZoneMesh& fzm = this->owner().mesh().faceZones();
374 
375  forAll(faceZoneIDs_, i)
376  {
377  const faceZone& fz = fzm[faceZoneIDs_[i]];
378 
379  label faceId = -1;
380  forAll(fz, j)
381  {
382  if (fz[j] == p.face())
383  {
384  faceId = j;
385  break;
386  }
387  }
388 
389  if (faceId != -1)
390  {
391  mass_[i][faceId] += p.mass()*p.nParticle();
392  }
393  }
394  }
395 }
396 
397 
398 // ************************************************************************* //
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
label faceId(-1)
const word & name() const
Return name.
Definition: IOobject.H:295
void inplaceRenumber(const labelUList &oldToNew, ListType &)
Inplace renumber the values of a list.
const faceZoneMesh & faceZones() const
Return face zone mesh.
Definition: polyMesh.H:476
void write()
Write post-processing info.
static const char tab
Definition: Ostream.H:264
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
const Boundary & boundaryField() const
Return const-reference to the boundary field.
Output to file stream.
Definition: OFstream.H:82
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
const labelList & patchID() const
Per boundary face label the patch index.
FacePostProcessing(const dictionary &dict, CloudType &owner, const word &modelName)
Construct from dictionary.
Base class for graphics format writing. Entry points are.
Definition: writer.H:78
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:626
autoPtr< globalIndex > mergePoints(labelList &pointToGlobal, labelList &uniquePoints) const
Helper for merging (collocated!) mesh point data.
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:239
virtual ~FacePostProcessing()
Destructor.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1131
virtual void postFace(const parcelType &p, bool &keepParticle)
Post-face hook.
stressControl lookup("compactNormalStress") >> compactNormalStress
dynamicFvMesh & mesh
label findZoneID(const word &zoneName) const
Find zone index given a name.
Definition: ZoneMesh.C:341
A class for handling words, derived from string.
Definition: word.H:59
Records particle face quantities on used-specified face zone.
const Type & value() const
Return const reference to value.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1394
const word & name() const
Return name.
Definition: zone.H:147
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
Foam::polyBoundaryMesh.
static const char nl
Definition: Ostream.H:265
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
label patchi
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
messageStream Info
tmp< pointField > allPoints(const Triangulation &t)
Extract all points in vertex-index order.
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
Selector class for relaxation factors, solver type and solution.
Definition: solution.H:48
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:64
volScalarField alpha(IOobject("alpha", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::AUTO_WRITE), lambda *max(Ua &U, zeroSensitivity))
volScalarField & p
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:66
Templated base class for dsmc cloud.
Definition: DSMCCloud.H:69
Templated cloud function object base class.
label whichFace(const label l) const
Return label of face in patch from global face label.
Definition: polyPatch.H:380