logFiles.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) 2012-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "logFiles.H"
30 #include "Time.H"
31 #include "IFstream.H"
32 
33 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
34 
36 {
37  if (Pstream::master())
38  {
39  const word startTimeName =
41 
42  forAll(names_, i)
43  {
44  if (!filePtrs_.set(i))
45  {
47 
49  }
50  }
51  }
52 }
53 
54 
56 {
57  names_.clear();
58  names_.append(names);
59 
60  if (Pstream::master())
61  {
62  filePtrs_.clear();
63  filePtrs_.setSize(names_.size());
64  }
65 
66  createFiles();
67 }
68 
69 
71 {
72  names_.clear();
73  names_.append(name);
74 
76 }
77 
78 
79 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
80 
81 Foam::functionObjects::logFiles::logFiles
82 (
83  const objectRegistry& obr,
84  const word& prefix
85 )
86 :
87  writeFile(obr, prefix),
88  names_(),
89  filePtrs_()
90 {}
91 
92 
93 Foam::functionObjects::logFiles::logFiles
94 (
95  const objectRegistry& obr,
96  const word& prefix,
97  const dictionary& dict
98 )
99 :
100  writeFile(obr, prefix),
101  names_(),
102  filePtrs_()
103 {
105 }
106 
107 
108 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
111 {
112  return names_;
113 }
114 
115 
117 {
118  if (!Pstream::master())
119  {
121  << "Request for files() can only be done by the master process"
123  }
124 
125  return filePtrs_;
126 }
127 
128 
130 {
131  if (!Pstream::master())
132  {
134  << "Request for file(i) can only be done by the master process"
135  << abort(FatalError);
136  }
137 
138  if (!filePtrs_.set(i))
139  {
141  << "File pointer at index " << i << " not allocated"
143  }
144 
145  return filePtrs_[i];
146 }
147 
148 
150 {
151  createFiles();
152 
153  return true;
154 }
155 
156 
157 // ************************************************************************* //
const Type & value() const noexcept
Return const reference to value.
dictionary dict
virtual bool write()
Write function.
Definition: logFiles.C:142
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
List of names generated by calling name() for each list item and filtered for matches.
virtual void createFiles()
Create the output file.
Definition: logFiles.C:28
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
wordList names_
File names.
Definition: logFiles.H:67
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
virtual void resetFile(const word &name)
Reset internal file pointer to new file with new name.
Definition: writeFile.C:165
Output to file stream, using an OSstream.
Definition: OFstream.H:49
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:789
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
PtrList< OFstream > & files()
Return access to the files.
Definition: logFiles.C:109
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
void initStream(Ostream &os) const
Initialise the output stream for writing.
Definition: writeFile.C:35
A class for handling words, derived from Foam::string.
Definition: word.H:63
const Time & time() const noexcept
Return time registry.
virtual void resetName(const word &name)
Reset the list of names to a single name entry.
Definition: logFiles.C:63
const wordList & names() const
Return const access to the names.
Definition: logFiles.C:103
errorManip< error > abort(error &err)
Definition: errorManip.H:139
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
virtual autoPtr< OFstream > newFileAtStartTime(const word &name) const
Return autoPtr to a new file using the simulation start time.
Definition: writeFile.C:156
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:241
List< word > wordList
List of word.
Definition: fileName.H:59
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
const objectRegistry & fileObr_
Reference to the region objectRegistry.
Definition: writeFile.H:121
Registry of regIOobjects.
Base class for writing single files from the function objects.
Definition: writeFile.H:112
PtrList< OFstream > filePtrs_
File pointer.
Definition: logFiles.H:72
virtual void resetNames(const wordList &names)
Reset the list of names from a wordList.
Definition: logFiles.C:48