xmgraceCoordSetWriter.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2021-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 "xmgraceCoordSetWriter.H"
30 #include "coordSet.H"
31 #include "fileName.H"
32 #include "OFstream.H"
33 #include "OSspecific.H"
34 #include "coordSetWriterMethods.H"
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41 namespace coordSetWriters
42 {
43  defineTypeName(xmgraceWriter);
44  addToRunTimeSelectionTable(coordSetWriter, xmgraceWriter, word);
45  addToRunTimeSelectionTable(coordSetWriter, xmgraceWriter, wordDict);
46 }
47 }
48 
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 // Implementation
54 
55 
56 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57 
59 :
61  streamOpt_(),
62  precision_(IOstream::defaultPrecision()),
63  ofile_(nullptr),
64  nWritten_(0)
65 {
66  buffering_ = true;
67 }
68 
69 
71 :
72  coordSetWriter(options),
73  streamOpt_
74  (
75  IOstreamOption::ASCII,
76  IOstreamOption::compressionEnum("compression", options)
77  ),
78  precision_
79  (
80  options.getOrDefault("precision", IOstream::defaultPrecision())
81  ),
82  ofile_(nullptr),
83  nWritten_(0)
84 {
85  buffering_ = options.getOrDefault("buffer", true);
86 }
87 
88 
90 (
91  const coordSet& coords,
92  const fileName& outputPath,
93  const dictionary& options
94 )
95 :
96  xmgraceWriter(options)
97 {
98  open(coords, outputPath);
99 }
100 
101 
103 (
104  const UPtrList<coordSet>& tracks,
105  const fileName& outputPath,
106  const dictionary& options
107 )
108 :
109  xmgraceWriter(options)
110 {
111  open(tracks, outputPath);
112 }
113 
114 
115 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
116 
118 {
119  close();
120 }
121 
122 
123 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
124 
126 {
127  const bool old(buffering_);
128  buffering_ = on;
129  return old;
130 }
131 
132 
134 {
135  // Assume !useTracks_, otherwise too fragile
136 
137  // 1) rootdir/<TIME>/setName.agr
138  // 2) rootdir/setName.agr
139 
140  return getExpectedPath("agr");
141 }
142 
143 
145 {
146  ofile_.reset(nullptr);
147  nWritten_ = 0;
148  coordSetWriter::close(force);
149 }
150 
151 
153 {
154  ofile_.reset(nullptr);
155  nWritten_ = 0;
157 }
158 
159 
161 {
162  ofile_.reset(nullptr);
163  nWritten_ = 0;
165 }
166 
167 
169 {
170  ofile_.reset(nullptr);
171  nWritten_ = 0;
173 }
174 
175 
176 template<class Type>
177 Foam::fileName Foam::coordSetWriters::xmgraceWriter::writeTemplate
178 (
179  const word& fieldName,
180  const Field<Type>& values
181 )
182 {
183  checkOpen();
184  if (coords_.empty())
185  {
186  return fileName::null;
187  }
188 
189  if (useTracks_ || !buffering_)
190  {
191  UPtrList<const Field<Type>> fieldPtrs(repackageFields(values));
192  return writeTemplate(fieldName, fieldPtrs);
193  }
194 
195 
196  // Regular version
197 
198  const auto& coords = coords_[0];
199 
200  if (!ofile_)
201  {
202  // Field:
203  // 1) rootdir/<TIME>/setName.agr
204  // 2) rootdir/setName.agr
205 
206  const fileName outputFile = path();
207 
208  if (!isDir(outputFile.path()))
209  {
210  mkDir(outputFile.path());
211  }
212 
213  ofile_.reset(new OFstream(outputFile, streamOpt_));
214  auto& os = ofile_();
215  os.precision(precision_);
216 
217  // Preamble
218  os << "@g0 on" << nl
219  << "@with g0" << nl
220  << "@ title \"" << coords.name() << '"' << nl
221  << "@ xaxis label \"" << coords.axis() << '"' << nl;
222 
223  nWritten_ = 0; // Restarted
224  }
225  auto& os = ofile_();
226 
227  // Plot entry
228  {
229  os << "@ s" << nWritten_
230  << " legend \"" << fieldName << '"' << nl
231  << "@target G0.S" << nWritten_ << nl;
232 
233  writeTable(os, coords, values, " \t");
234 
235  os << '&' << nl;
236  os << "# end_data" << nl;
237  ++nWritten_;
238  }
240  return ofile_().name();
241 }
242 
243 
244 template<class Type>
245 Foam::fileName Foam::coordSetWriters::xmgraceWriter::writeTemplate
246 (
247  const word& fieldName,
248  const List<Field<Type>>& fieldValues
249 )
250 {
251  checkOpen();
252  if (coords_.empty())
253  {
254  return fileName::null;
255  }
256  useTracks_ = true; // Extra safety
257 
258  UPtrList<const Field<Type>> fieldPtrs(repackageFields(fieldValues));
259  return writeTemplate(fieldName, fieldPtrs);
260 }
261 
262 
263 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
264 
265 // Field writing methods
267 
268 
269 // ************************************************************************* //
addToRunTimeSelectionTable(coordSetWriter, abaqusWriter, word)
A class for handling file names.
Definition: fileName.H:72
const word & axis() const
The sort axis name.
Definition: coordSet.H:160
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
static const fileName null
An empty fileName.
Definition: fileName.H:111
virtual bool buffering() const
True if the format uses internal buffering (eg, column output)
defineTypeName(abaqusWriter)
Write coordSet(s) in xmgrace format.
virtual ~xmgraceWriter()
Destructor. Calls close()
A simple container for options an IOstream can normally have.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
defineCoordSetWriterWriteFields(Foam::coordSetWriters::xmgraceWriter)
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:860
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
virtual void beginTime(const Time &t)
Begin a time-step.
Convenience macros for instantiating coordSetWriter methods.
virtual fileName path() const
Characteristic output file name - information only.
Holds list of sampling positions.
Definition: coordSet.H:49
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:614
Generic templated field type.
Definition: Field.H:62
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual void close(bool force=false)
Close and reset, clears backend.
Base class for writing coordSet(s) and tracks with fields.
const word & name() const noexcept
The coord-set name.
Definition: coordSet.H:152
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
OBJstream os(runTime.globalPath()/outputName)
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
virtual void endTime()
End time step. Clears existing backend.
virtual void endTime()
End a time-step.
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:82
virtual void beginTime(const Time &t)
Begin time step. Clears existing backend.
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
virtual void close(bool force=false)
Finish output, performing any necessary cleanup.
Namespace for OpenFOAM.