Loading...
Searching...
No Matches
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-2024 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#include "histogramModel.H"
29#include "fvMesh.H"
30#include "ListOps.H"
31
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace 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 (
66 (
67 fieldName,
68 mesh_.time().timeName(),
69 mesh_.thisDb(),
73 ),
74 mesh_
75 );
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]
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// ************************************************************************* //
Various functions to operate on Lists.
@ REGISTER
Request registration (bool: true).
@ MUST_READ
Reading required.
@ AUTO_WRITE
Automatically write from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
const Type & value() const noexcept
Return const reference to value.
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition writeFile.C:334
writeFile(const objectRegistry &obr, const fileName &prefix, const word &name="undefined", const bool writeToFile=true, const string &ext=".dat")
Construct from objectRegistry, prefix, fileName.
Definition writeFile.C:200
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition writeFile.C:344
virtual bool read(const dictionary &dict)
Read.
Definition writeFile.C:240
bool writtenHeader_
Flag to identify whether the header has been written.
Definition writeFile.H:157
virtual OFstream & file()
Return access to the file (if only 1).
Definition writeFile.C:270
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
Mesh data needed to do the Finite Volume discretisation.
Definition fvMesh.H:85
const Time & time() const
Return the top-level database.
Definition fvMesh.H:360
A base class for histogram models.
const fvMesh & mesh() const noexcept
Return const reference to the mesh.
volScalarField & getOrReadField(const word &fieldName) const
Return requested field from the object registry or read+register the field to the object registry.
void write(scalarField &dataNormalised, const labelField &dataCount, const scalarField &magMidBin)
Write histogram data.
virtual bool read(const dictionary &dict)
Read the function-object dictionary.
virtual void writeFileHeader(Ostream &os)
Output file header information.
const word & fieldName() const noexcept
Return const reference to the operand field name.
histogramModel(const word &name, const fvMesh &mesh, const dictionary &dict)
Construct from components.
bool store()
Register object with its registry and transfer ownership to the registry.
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
dynamicFvMesh & mesh
OBJstream os(runTime.globalPath()/outputName)
auto & name
Namespace for OpenFOAM.
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Field< label > labelField
Specialisation of Field<T> for label.
Definition labelField.H:48
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1, const label comm)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
constexpr char tab
The tab '\t' character(0x09).
Definition Ostream.H:49
#define defineRunTimeSelectionTable(baseType, argNames)
Define run-time selection table.
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299