gnuplotSetWriter.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "gnuplotSetWriter.H"
27 #include "coordSet.H"
28 #include "fileName.H"
29 #include "OFstream.H"
30 #include "OSspecific.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
40 }
41 
42 
43 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
44 
46 {}
47 
48 
49 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
50 
52 (
53  const fileName& outputDir,
54  const fileName& setName,
55  const coordSet& set,
56  const wordList& valueSetNames
57  #define TypeValueSetsConstArg(Type, nullArg) \
58  , const UPtrList<const Field<Type>>& Type##ValueSets
61 ) const
62 {
63  if (!set.hasScalarAxis())
64  {
66  << "Cannot write " << setName << " in " << typeName
67  << " format as it does not have a scalar axis"
68  << exit(FatalError);
69  }
70 
71  if (!isDir(outputDir))
72  {
73  mkDir(outputDir);
74  }
75 
76  OFstream os
77  (
78  outputDir/setName + ".gplt",
81  writeCompression_
82  );
83 
84  os << "$data << EOD" << nl;
85 
86  writeTable
87  (
89  ? coordSet
90  (
91  set.segments(),
92  word::null,
94  set.scalarName(),
95  set.scalarCoords()
96  )
97  : set,
98  #define TypeValueSetsParameter(Type, nullArg) Type##ValueSets,
101  os
102  );
103 
104  os << nl << "EOD" << nl << nl;
105 
106  os << "set term postscript color" << nl
107  << "set output \"" << setName.c_str() << ".ps\"" << nl
108  << "set xlabel \"" << set.scalarName() << "\"" << nl;
109 
110  if (valueSetNames.size() == 2)
111  {
112  os << "set ytics nomirror" << nl
113  << "set ylabel \"" << valueSetNames[0] << "\"" << nl
114  << "set y2tics nomirror" << nl
115  << "set y2label \"" << valueSetNames[1] << "\"" << nl;
116  }
117 
118  os << nl << "plot ";
119 
120  label columni = 0;
121 
122  forAll(valueSetNames, fieldi)
123  {
124  #define PlotTypeValueSets(Type, nullArg) \
125  if (Type##ValueSets.set(fieldi)) \
126  { \
127  for \
128  ( \
129  direction cmpt = 0; \
130  cmpt < pTraits<Type>::nComponents; \
131  cmpt++ \
132  ) \
133  { \
134  const bool separator = \
135  !valueSetNames[fieldi].empty() \
136  && strlen(pTraits<Type>::componentNames[cmpt]) > 0; \
137  \
138  const word w = \
139  valueSetNames[fieldi] \
140  + (separator ? "_" : "") \
141  + pTraits<Type>::componentNames[cmpt]; \
142  \
143  if (columni != 0) os << ", "; \
144  \
145  os << "$data us 1:" << 2 + columni; \
146  \
147  if (valueSetNames.size() == 2) \
148  { \
149  os << " axis x1y" << 1 + fieldi; \
150  } \
151  \
152  os << " title \"" << w << "\" with lines"; \
153  \
154  ++ columni; \
155  } \
156  }
158  #undef PlotTypeValueSets
159  }
160 }
161 
162 
163 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Macros for easy insertion into run-time selection tables.
Pre-declare SubField and related Field type.
Definition: Field.H:82
static const Field< vector > & null()
Return a null field.
Definition: Field.H:111
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
Output to file stream.
Definition: OFstream.H:86
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:66
Holds list of sampling positions.
Definition: coordSet.H:51
static const NamedEnum< axisType, 6 > axisTypeNames_
String representation of axis enums.
Definition: coordSet.H:69
word axis() const
Return the axis name.
Definition: coordSet.H:147
tmp< scalarField > scalarCoords() const
Get scalar coordinates (axis is x, y, z or distance)
Definition: coordSet.C:193
word scalarName() const
Return the name of the scalar coordinates.
Definition: coordSet.C:223
const labelList & segments() const
Return the segments.
Definition: coordSet.H:141
bool hasScalarAxis() const
Is the coordinate axis a scalar?
Definition: coordSet.C:138
A class for handling file names.
Definition: fileName.H:82
Write set in gnuplot format.
virtual void write(const fileName &outputDir, const fileName &setName, const coordSet &set, const wordList &valueSetNames #define TypeValueSetsConstArg(Type, nullArg)) const
Write a coordSet and associated data.
virtual ~gnuplotSetWriter()
Destructor.
Base class for writing coordinate sets with data.
Definition: setWriter.H:64
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
#define PlotTypeValueSets(Type, nullArg)
#define TypeValueSetsParameter(Type, nullArg)
#define TypeValueSetsConstArg(Type, nullArg)
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
FOR_ALL_FIELD_TYPES(DefineContiguousFvWallLocationDataType)
defineTypeNameAndDebug(combustionModel, 0)
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
error FatalError
static const char nl
Definition: Ostream.H:260
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
dictionary dict