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 -------------------------------------------------------------------------------
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 "pointHistory.H"
30 #include "IOmanip.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(pointHistory, 0);
37 
39  (
40  functionObject,
41  pointHistory,
42  dictionary
43  );
44 }
45 
46 
47 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48 
49 bool Foam::pointHistory::writeData()
50 {
51  const fvMesh& mesh =
53 
54  vector position(Zero);
55 
56  if (processor_ == Pstream::myProcNo())
57  {
58  position = mesh.points()[historyPointID_];
59  }
60 
61  reduce(position, sumOp<vector>());
62 
63  if (Pstream::master())
64  {
65  historyFilePtr_() << setprecision(12);
66 
67  historyFilePtr_()
68  << time_.time().value() << tab
69  << position.x() << tab
70  << position.y() << tab
71  << position.z() << endl;
72  }
73 
74  return true;
75 }
76 
77 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
78 
79 Foam::pointHistory::pointHistory
80 (
81  const word& name,
82  const Time& runTime,
83  const dictionary& dict
84 )
85 :
87  name_(name),
88  time_(runTime),
89  regionName_(polyMesh::defaultRegion),
90  historyPointID_(-1),
91  refHistoryPoint_(dict.lookup("refHistoryPoint")),
92  processor_(-1),
93  fileName_(dict.get<word>("fileName")),
94  historyFilePtr_(nullptr)
95 {
96  Info<< "Creating " << this->name() << " function object." << endl;
97 
98  dict.readIfPresent("region", regionName_);
99  dict.readIfPresent("historyPointID", historyPointID_);
100 
101  const fvMesh& mesh =
102  time_.lookupObject<fvMesh>(regionName_);
103 
104  const vectorField& points = mesh.points();
105 
106  List<scalar> minDist(Pstream::nProcs(), GREAT);
107 
108  if (historyPointID_ == -1)
109  {
110  forAll(points, pointI)
111  {
112  scalar dist = mag(refHistoryPoint_ - points[pointI]);
113 
114  if (dist < minDist[Pstream::myProcNo()])
115  {
116  minDist[Pstream::myProcNo()] = dist;
117  historyPointID_ = pointI;
118  }
119  }
120  }
121 
122  Pstream::gatherList(minDist);
123  Pstream::scatterList(minDist);
124 
125  processor_ = -1;
126  scalar min = GREAT;
127 
128  forAll(minDist, procI)
129  {
130  if (minDist[procI] < min)
131  {
132  min = minDist[procI];
133  processor_ = procI;
134  }
135  }
136 
137  if (processor_ == Pstream::myProcNo())
138  {
139  Pout<< "History point ID: " << historyPointID_ << nl
140  << "History point coordinates: "
141  << points[historyPointID_] << nl
142  << "Reference point coordinates: " << refHistoryPoint_
143  << endl;
144  }
145 
146  // Create history file if not already created
147  if (!historyFilePtr_)
148  {
149  // File update
150  if (Pstream::master())
151  {
152  fileName historyDir;
153 
154  word startTimeName =
155  time_.timeName(mesh.time().startTime().value());
156 
157  if (Pstream::parRun())
158  {
159  // Put in undecomposed case (Note: gives problems for
160  // distributed data running)
161  historyDir = time_.path()/".."/"history"/startTimeName;
162  }
163  else
164  {
165  historyDir = time_.path()/"history"/startTimeName;
166  }
167 
168  // Create directory if does not exist.
169  mkDir(historyDir);
170 
171 
172  // Open new file at start up
173 
174  // OStringStream FileName;
175  // FileName() << "point_" << historyPointID_ << ".dat";
176 
177  historyFilePtr_.reset
178  (
179  new OFstream(historyDir/fileName_)
180  );
181 
182  // Add headers to output data
183  if (historyFilePtr_)
184  {
185  historyFilePtr_()
186  << "# Time" << tab << "X" << tab << "Y" << tab << "Z"
187  << endl;
188  }
189  }
190  }
191 
192  // Write start time data
193  writeData();
194 }
195 
196 
197 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
198 
200 {
201  return writeData();
202 }
203 
204 
206 {
207  dict.readIfPresent("region", regionName_);
208 
209  return true;
210 }
211 
212 
213 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::polyMesh::points
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1069
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
pointHistory.H
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:318
Foam::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::Pstream::scatterList
static void scatterList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Scatter data. Reverse of gatherList.
Definition: gatherScatterList.C:215
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::dimensioned::value
const Type & value() const
Return const reference to value.
Definition: dimensionedType.C:434
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:332
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::pointHistory::execute
virtual bool execute()
execute is called at each ++ or += of the time-loop
Definition: pointHistory.C:199
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:51
Foam::Field< vector >
Foam::pointHistory::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data has changed.
Definition: pointHistory.C:205
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
IOmanip.H
Istream and Ostream manipulators taking arguments.
Foam::objectRegistry::lookupObject
const Type & lookupObject(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:434
Foam::dictionary::lookup
ITstream & lookup(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionary.C:386
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::writeData
static void writeData(Ostream &os, const Type &val)
Definition: rawSurfaceWriterImpl.C:45
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:51
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::setprecision
Omanip< int > setprecision(const int i)
Definition: IOmanip.H:205
Foam::Pstream::gatherList
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
Definition: gatherScatterList.C:52
Foam::tab
constexpr char tab
Definition: Ostream.H:403
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:463
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::UPstream::parRun
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
Foam::List< scalar >
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:280
Foam::mkDir
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:507
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::Time::startTime
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:867
Foam::objectRegistry::time
const Time & time() const noexcept
Return time registry.
Definition: objectRegistry.H:178
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:445
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:405