Loading...
Searching...
No Matches
gnuplotCoordSetWriter.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) 2017-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
30#include "coordSet.H"
31#include "fileName.H"
32#include "OFstream.H"
33#include "OSspecific.H"
37// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39namespace Foam
40{
41namespace coordSetWriters
42{
46}
47}
48
49
50// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52// Implementation
54
55
56// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57
59:
61 streamOpt_(),
62 precision_(IOstream::defaultPrecision())
63{
64 buffering_ = true;
65}
66
67
69:
70 coordSetWriter(options),
71 streamOpt_
72 (
73 IOstreamOption::ASCII,
74 IOstreamOption::compressionEnum("compression", options)
75 ),
76 precision_
77 (
78 options.getOrDefault("precision", IOstream::defaultPrecision())
79 )
80{
81 buffering_ = options.getOrDefault("buffer", true);
82}
83
84
86(
87 const coordSet& coords,
88 const fileName& outputPath,
89 const dictionary& options
90)
92 gnuplotWriter(options)
93{
94 open(coords, outputPath);
95}
96
97
99(
100 const UPtrList<coordSet>& tracks,
101 const fileName& outputPath,
102 const dictionary& options
103)
104:
105 gnuplotWriter(options)
107 open(tracks, outputPath);
108}
109
110
111// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
112
115 close();
116}
117
118
119// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120
123 const bool old(buffering_);
124 buffering_ = on;
125 return old;
126}
127
128
130{
131 // 1) rootdir/<TIME>/setName.{gplt}
132 // 2) rootdir/setName.{gplt}
133
134 return getExpectedPath("gplt");
135}
136
137
139{
140 if (coords_.empty())
141 {
142 clearBuffers();
143 return false;
144 }
145
146 // Field:
147 // 1) rootdir/<TIME>/setName.gplt
148 // 2) rootdir/setName.gplt
149
150 fileName outputFile = path();
151
152 if (!isDir(outputFile.path()))
153 {
154 mkDir(outputFile.path());
155 }
156
157 OFstream os(outputFile, streamOpt_);
158 os.precision(precision_);
159
160 os << "set term pngcairo" << nl
161 << "set output \"" << outputFile.stem() << ".png\"" << nl;
162
163 label nplots = 0;
164 do
165 {
166 #undef doLocalCode
167 #define doLocalCode(Type) \
168 for (const word& fldName : Type##Names_) \
169 { \
170 os << (nplots++ ? ", \\" : "plot \\") << nl; \
171 os << " '-' title \"" << fldName << "\" with lines"; \
172 }
173
174 doLocalCode(label);
175 doLocalCode(scalar);
180 #undef doLocalCode
181 }
182 while (false);
183
184 os << nl << nl;
185
186 if (nplots)
187 {
188 #undef doLocalCode
189 #define doLocalCode(Type) \
190 for (const Field<Type>& fld : Type##Fields_) \
191 { \
192 writeTable(os, coords_[0], fld, " \t"); \
193 os << "end_data" << nl << nl; \
194 }
195
196 doLocalCode(label);
197 doLocalCode(scalar);
202 #undef doLocalCode
203 }
204
205 os << "# end plot" << nl;
206
208
209 return true;
210}
211
212
213template<class Type>
214Foam::fileName Foam::coordSetWriters::gnuplotWriter::writeTemplate
215(
216 const word& fieldName,
217 const Field<Type>& values
218)
219{
220 checkOpen();
221 if (coords_.empty())
222 {
223 return fileName::null;
224 }
225
226 if (useTracks_ || !buffering_)
227 {
228 UPtrList<const Field<Type>> fieldPtrs(repackageFields(values));
229 return writeTemplate(fieldName, fieldPtrs);
230 }
231
232
233 // Buffering version
234 appendField(fieldName, values);
235
236 return path();
237}
238
239
240template<class Type>
241Foam::fileName Foam::coordSetWriters::gnuplotWriter::writeTemplate
242(
243 const word& fieldName,
244 const List<Field<Type>>& fieldValues
245)
246{
247 checkOpen();
248 if (coords_.empty())
249 {
250 return fileName::null;
251 }
252
253 UPtrList<const Field<Type>> fieldPtrs(repackageFields(fieldValues));
254 return writeTemplate(fieldName, fieldPtrs);
255}
256
257
258// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259
260// Field writing methods
261defineCoordSetWriterWriteFields(Foam::coordSetWriters::gnuplotWriter);
262
263
264// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
A simple container for options an IOstream can normally have.
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition IOstream.H:85
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
Base class for writing coordSet(s) and tracks with fields.
void appendField(const word &fieldName, const Field< label > &vals)
virtual void open(const fileName &outputPath)
Write separate geometry to file.
fileName getExpectedPath(const word &fileExt=word::null) const
Get expected (characteristic) output file name - information only.
bool buffering_
Writer with buffering output.
virtual bool buffering() const
True if the format uses internal buffering (eg, column output).
static UPtrList< const Field< Type > > repackageFields(const Field< Type > &field)
Repackage field into a UPtrList.
void checkOpen() const
Verify that the outputPath_ has been set or FatalError.
coordSetWriter(const coordSetWriter &)=delete
No copy construct.
bool useTracks_
Prefer tracks to points during single set writing.
void clearBuffers()
Clear out buffering.
UPtrList< const coordSet > coords_
Reference to coordinate set(s).
virtual void close(bool force=false)
Finish output, performing any necessary cleanup.
A coordSet(s) writer in gnuplot format.
virtual bool writeBuffered()
Write buffered data.
virtual fileName path() const
Characteristic output file name - information only.
virtual ~gnuplotWriter()
Destructor. Calls close().
Holds list of sampling positions.
Definition coordSet.H:52
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
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...
A class for handling file names.
Definition fileName.H:75
static const fileName null
An empty fileName.
Definition fileName.H:111
static std::string stem(const std::string &str)
Return the basename, without extension.
Definition fileName.C:391
static std::string path(const std::string &str)
Return directory path name (part before last /).
Definition fileNameI.H:169
Tensor of scalars, i.e. Tensor<scalar>.
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:66
#define defineTypeName(Type)
Define the typeName.
Definition className.H:113
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Convenience macros for instantiating coordSetWriter methods.
#define defineCoordSetWriterWriteFields(ThisClass)
OBJstream os(runTime.globalPath()/outputName)
#define doLocalCode(FieldType, Variable)
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 POSIX.C:616
Tensor< scalar > tensor
Definition symmTensor.H:57
Vector< scalar > vector
Definition vector.H:57
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars, i.e. SphericalTensor<scalar>.
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition POSIX.C:862
SymmTensor< scalar > symmTensor
SymmTensor of scalars, i.e. SymmTensor<scalar>.
Definition symmTensor.H:55
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50