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_,
72  ),
73  mesh_
74  );
75  mesh_.objectRegistry::store(ptr);
76  }
77 
78  return *ptr;
79 }
80 
81 
83 (
84  scalarField& dataNormalised,
85  const labelField& dataCount,
86  const scalarField& magBinMidPoint
87 )
88 {
89  if (!Pstream::master())
90  {
91  return;
92  }
93 
94  const scalar sumData = sum(dataNormalised);
95 
96  if (sumData < SMALL)
97  {
98  return;
99  }
100 
101  dataNormalised /= sumData;
102 
103  const auto time = mesh().time().value();
104 
105  forAll(dataNormalised, i)
106  {
107  file()
108  << time << tab
109  << magBinMidPoint[i] << tab
110  << dataCount[i] << tab
111  << dataNormalised[i]
112  << endl;
113  }
114 }
115 
116 
117 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
118 
120 (
121  const word& name,
122  const fvMesh& mesh,
123  const dictionary& dict
124 )
125 :
126  writeFile(mesh, name, "histogram", dict),
127  mesh_(mesh),
128  fieldName_(word::null)
129 {}
130 
131 
132 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133 
135 {
137  {
138  return false;
139  }
140 
141  fieldName_ = dict.get<word>("field");
142 
143  if (writeToFile() && !writtenHeader_)
144  {
145  writeFileHeader(file());
146  }
147 
148  return true;
149 }
150 
151 
152 // ************************************************************************* //
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
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:120
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:487
constexpr char tab
The tab &#39;\t&#39; character(0x09)
Definition: Ostream.H:48
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:361
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Various functions to operate on Lists.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:413
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:84
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 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.
A base class for histogram models.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
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)
Am I the master rank.
Definition: UPstream.H:672
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:166
Namespace for OpenFOAM.
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:329