histogramModel.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) 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 #include "histogramModel.H"
29 #include "fvMesh.H"
30 #include "ListOps.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
38 }
39 
40 
41 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
42 
44 {
45  writeHeader(os, "Histogram");
46  writeCommented(os, "Time");
47  writeTabbed(os, "binMidPoints");
48  writeTabbed(os, "dataCounts");
49  writeTabbed(os, "dataValues");
50  os << endl;
51 }
52 
53 
55 (
56  const word& fieldName
57 ) const
58 {
59  auto* ptr = mesh_.getObjectPtr<volScalarField>(fieldName);
60 
61  if (!ptr)
62  {
63  ptr = new volScalarField
64  (
65  IOobject
66  (
67  fieldName,
68  mesh_.time().timeName(),
69  mesh_,
73  ),
74  mesh_
75  );
76  mesh_.objectRegistry::store(ptr);
77  }
78 
79  return *ptr;
80 }
81 
82 
84 (
85  scalarField& dataNormalised,
86  const labelField& dataCount,
87  const scalarField& magBinMidPoint
88 )
89 {
90  if (!Pstream::master())
91  {
92  return;
93  }
94 
95  const scalar sumData = sum(dataNormalised);
96 
97  if (sumData < SMALL)
98  {
99  return;
100  }
101 
102  dataNormalised /= sumData;
103 
104  const auto time = mesh().time().value();
105 
106  forAll(dataNormalised, i)
107  {
108  file()
109  << time << tab
110  << magBinMidPoint[i] << tab
111  << dataCount[i] << tab
112  << dataNormalised[i]
113  << endl;
114  }
115 }
116 
117 
118 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
119 
121 (
122  const word& name,
123  const fvMesh& mesh,
124  const dictionary& dict
125 )
126 :
127  writeFile(mesh, name, "histogram", dict),
128  mesh_(mesh),
129  fieldName_()
130 {}
131 
132 
133 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
134 
136 {
138  {
139  return false;
140  }
141 
142  fieldName_ = dict.get<word>("field");
143 
144  if (writeToFile() && !writtenHeader_)
145  {
146  writeFileHeader(file());
147  }
148 
149  return true;
150 }
151 
152 
153 // ************************************************************************* //
void write(scalarField &dataNormalised, const labelField &dataCount, const scalarField &magMidBin)
Write histogram data.
const Type & value() const noexcept
Return const reference to value.
dictionary dict
Field< label > labelField
Specialisation of Field<T> for label.
Definition: labelField.H:48
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1)
virtual void writeFileHeader(Ostream &os)
Output file header information.
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:339
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
virtual bool read(const dictionary &dict)
Read top-level dictionary.
histogramModel(const word &name, const fvMesh &mesh, const dictionary &dict)
Construct from components.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
constexpr char tab
The tab &#39;\t&#39; character(0x09)
Definition: Ostream.H:49
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:360
Various functions to operate on Lists.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:81
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:313
dynamicFvMesh & mesh
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
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
A base class for histogram models.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
OBJstream os(runTime.globalPath()/outputName)
defineTypeNameAndDebug(combustionModel, 0)
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:241
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
volScalarField & getOrReadField(const word &fieldName) const
Return requested field from the object registry or read+register the field to the object registry...
Automatically write from objectRegistry::writeObject()
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
Request registration (bool: true)
Namespace for OpenFOAM.
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:329