xmgraceCoordSetWriterImpl.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) 2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "IOmanip.H"
29 #include "OFstream.H"
30 #include "OSspecific.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class Type>
35 Foam::fileName Foam::coordSetWriters::xmgraceWriter::writeTemplate
36 (
37  const word& fieldName,
38  const UPtrList<const Field<Type>>& fieldPtrs
39 )
40 {
41  if (coords_.size() != fieldPtrs.size())
42  {
44  << "Attempted to write field: " << fieldName
45  << " (" << fieldPtrs.size() << " entries) for "
46  << coords_.size() << " sets" << nl
47  << exit(FatalError);
48  }
49 
50 
51  // Field: rootdir/<TIME>/<field>_setName.agr
52 
53  fileName outputFile = getFieldPrefixedPath(fieldName, "agr");
54 
55  if (verbose_)
56  {
57  Info<< "Writing field " << fieldName;
58  Info<< " to " << outputFile << endl;
59  }
60 
61  // Master only
62  {
63  if (!isDir(outputFile.path()))
64  {
65  mkDir(outputFile.path());
66  }
67 
68  OFstream os(outputFile, streamOpt_);
69  os.precision(precision_);
70 
71  // Preamble
72  {
73  const coordSet& coords = coords_[0];
74 
75  os << "@g0 on" << nl
76  << "@with g0" << nl
77  << "@ title \"" << coords.name() << '"' << nl
78  << "@ xaxis label \"" << coords.axis() << '"' << nl;
79  }
80 
81  const label setNumber = 0;
82 
83  // Plot entry
84  os << "@ s" << setNumber
85  << " legend \"" << fieldName << '"' << nl
86  << "@target G0.S" << setNumber << nl;
87 
88  forAll(coords_, tracki)
89  {
90  writeTable(os, coords_[tracki], fieldPtrs[tracki], " \t");
91  }
92 
93  os << '&' << nl;
94  os << "# end_data" << nl;
95  }
96 
97  wroteGeom_ = true;
98  return outputFile;
99 }
100 
101 
102 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
const word & axis() const
The sort axis name.
Definition: coordSet.H:160
bool wroteGeom_
Track if geometry has been written since the last open.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
Output to file stream, using an OSstream.
Definition: OFstream.H:49
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:169
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:860
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: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
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
Istream and Ostream manipulators taking arguments.
OBJstream os(runTime.globalPath()/outputName)
static void writeTable(Ostream &os, const coordSet &coords, const UList< Type > &values, const char *sep)
Write coordinates and values.
fileName getFieldPrefixedPath(const word &fieldName, const word &fileExt=word::null) const
Get field-prefixed output file name.
UPtrList< const coordSet > coords_
Reference to coordinate set(s)
bool verbose_
Additional output verbosity.
messageStream Info
Information stream (stdout output on master, null elsewhere)