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 {
37  defineTypeNameAndDebug(gnuplotSetWriter, 0);
38  addToRunTimeSelectionTable(setWriter, gnuplotSetWriter, word);
39  addToRunTimeSelectionTable(setWriter, gnuplotSetWriter, dict);
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
59  FOR_ALL_FIELD_TYPES(TypeValueSetsConstArg)
60  #undef TypeValueSetsConstArg
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 // ************************************************************************* //
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
#define TypeValueSetsConstArg(Type, nullArg)
Output to file stream.
Definition: OFstream.H:82
static const NamedEnum< axisType, 6 > axisTypeNames_
String representation of axis enums.
Definition: coordSet.H:69
Macros for easy insertion into run-time selection tables.
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.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Holds list of sampling positions.
Definition: coordSet.H:50
static word scalarName
Definition: globalFoam.H:32
virtual ~gnuplotSetWriter()
Destructor.
Pre-declare SubField and related Field type.
Definition: Field.H:56
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
static const word null
An empty word.
Definition: word.H:77
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:54
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
static const char nl
Definition: Ostream.H:260
defineTypeNameAndDebug(combustionModel, 0)
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
#define TypeValueSetsParameter(Type, nullArg)
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
static const Field< Type > & null()
Return a null field.
Definition: Field.H:111
FOR_ALL_FIELD_TYPES(DefineFvWallInfoType)
Namespace for OpenFOAM.
#define PlotTypeValueSets(Type, nullArg)