pointHistory.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) 2019 Zeljko Tukovic, FSB Zagreb.
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 "pointHistory.H"
30#include "IOmanip.H"
31
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace Foam
35{
37
39 (
43 );
44}
45
46
47// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48
49bool Foam::pointHistory::writeData()
50{
51 const fvMesh& mesh = time_.lookupObject<fvMesh>(polyMesh::defaultRegion);
52
53 vector position(Zero);
54
55 if (processor_ == Pstream::myProcNo())
56 {
57 position = mesh.points()[historyPointID_];
58 }
59
60 reduce(position, sumOp<vector>());
61
62 if (Pstream::master())
63 {
64 historyFilePtr_() << setprecision(12);
65
66 historyFilePtr_()
67 << time_.time().value() << tab
68 << position.x() << tab
69 << position.y() << tab
70 << position.z() << endl;
71 }
72
73 return true;
74}
75
76// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
77
79(
80 const word& name,
81 const Time& runTime,
82 const dictionary& dict
83)
84:
86 name_(name),
87 time_(runTime),
88 regionName_(polyMesh::defaultRegion),
89 historyPointID_(-1),
90 refHistoryPoint_(dict.lookup("refHistoryPoint")),
91 processor_(-1),
92 fileName_(dict.get<word>("fileName")),
93 historyFilePtr_(nullptr)
94{
95 Info<< "Creating " << this->name() << " function object." << endl;
96
97 dict.readIfPresent("region", regionName_);
98 dict.readIfPresent("historyPointID", historyPointID_);
99
100 const fvMesh& mesh = time_.lookupObject<fvMesh>(regionName_);
101
102 const vectorField& points = mesh.points();
103
104 List<scalar> minDist(Pstream::nProcs(), GREAT);
105
106 if (historyPointID_ == -1)
107 {
108 forAll(points, pointI)
109 {
110 scalar dist = mag(refHistoryPoint_ - points[pointI]);
111
112 if (dist < minDist[Pstream::myProcNo()])
113 {
114 minDist[Pstream::myProcNo()] = dist;
115 historyPointID_ = pointI;
116 }
117 }
118 }
119
120 Pstream::allGatherList(minDist);
121
122 processor_ = -1;
123 scalar min = GREAT;
124
125 forAll(minDist, procI)
126 {
127 if (minDist[procI] < min)
128 {
129 min = minDist[procI];
130 processor_ = procI;
131 }
132 }
133
134 if (processor_ == Pstream::myProcNo())
135 {
136 Pout<< "History point ID: " << historyPointID_ << nl
137 << "History point coordinates: "
138 << points[historyPointID_] << nl
139 << "Reference point coordinates: " << refHistoryPoint_
140 << endl;
141 }
142
143 // Create history file if not already created
144 if (!historyFilePtr_)
145 {
146 // File update
147 if (Pstream::master())
148 {
149 fileName historyDir;
150
151 word startTimeName =
152 time_.timeName(mesh.time().startTime().value());
153
154 if (Pstream::parRun())
155 {
156 // Put in undecomposed case (Note: gives problems for
157 // distributed data running)
158 historyDir = time_.path()/".."/"history"/startTimeName;
159 }
160 else
161 {
162 historyDir = time_.path()/"history"/startTimeName;
163 }
164
165 // Create directory if does not exist.
166 mkDir(historyDir);
167
168
169 // Open new file at start up
170
171 // OStringStream FileName;
172 // FileName() << "point_" << historyPointID_ << ".dat";
173
174 historyFilePtr_.reset
175 (
176 new OFstream(historyDir/fileName_)
177 );
178
179 // Add headers to output data
180 if (historyFilePtr_)
181 {
182 historyFilePtr_()
183 << "# Time" << tab << "X" << tab << "Y" << tab << "Z"
184 << endl;
185 }
186 }
187 }
188
189 // Write start time data
190 writeData();
191}
192
193
194// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
195
197{
198 return writeData();
199}
200
201
203{
204 dict.readIfPresent("region", regionName_);
205
206 return true;
207}
208
209
210// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
Output to file stream, using an OSstream.
Definition: OFstream.H:57
label nProcs() const noexcept
Number of ranks associated with PstreamBuffers.
static void allGatherList(const List< commsStruct > &comms, List< T > &values, const int tag, const label comm)
virtual bool read()
Re-read model coefficients if they have changed.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:861
fileName path() const
Return path.
Definition: Time.H:358
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
const Type & value() const
Return const reference to value.
A class for handling file names.
Definition: fileName.H:76
Abstract base-class for Time/database function objects.
const word & name() const noexcept
Return the name of this functionObject.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:91
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:290
const Time & time() const noexcept
Return time registry.
const Type & lookupObject(const word &name, const bool recursive=false) const
virtual bool execute()
execute is called at each ++ or += of the time-loop
Definition: pointHistory.C:196
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:321
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1083
int myProcNo() const noexcept
Return processor number.
Lookup type of boundary radiation properties.
Definition: lookup.H:66
splitCell * master() const
Definition: splitCell.H:113
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
dynamicFvMesh & mesh
engineTime & runTime
const pointField & points
Namespace for OpenFOAM.
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:515
messageStream Info
Information stream (stdout output on master, null elsewhere)
Omanip< int > setprecision(const int i)
Definition: IOmanip.H:205
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
static void writeData(Ostream &os, const Type &val)
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
constexpr char tab
The tab '\t' character(0x09)
Definition: Ostream.H:52
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333