csvCoordSetWriterImpl.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-------------------------------------------------------------------------------
10License
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// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
33
34namespace Foam
35{
36 // Output coordinate header
37 static void writeCoordHeader
38 (
39 Ostream& os,
40 const coordSet& coords,
41 const label count /* (future?) */
42 )
43 {
44 if (coords.hasVectorAxis())
45 {
46 os << "x,y,z";
47 }
48 else
49 {
50 // axis name
51 // const word axisName(coords.axis());
52 os << word(coords.axis());
53 }
54 }
55
56 // Write field name, use named components for VectorSpace
57 template<class Type>
58 static inline void writeHeader(Ostream& os, const word& fieldName)
59 {
61
62 const auto nCmpts(pTraits<Type>::nComponents);
63
64 if (pTraits<Type>::rank || nCmpts > 1)
65 {
66 for (direction d = 0; d < nCmpts; ++d)
67 {
68 os << ',' << fieldName
70 }
71 }
72 else
73 {
74 os << ',' << fieldName;
75 }
76 }
77
78} // End namespace Foam
79
80
81// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
82
83template<class Type>
84Foam::fileName Foam::coordSetWriters::csvWriter::writeTemplate
85(
86 const word& fieldName,
87 const UPtrList<const Field<Type>>& fieldPtrs
88)
89{
90 if (coords_.size() != fieldPtrs.size())
91 {
93 << "Attempted to write field: " << fieldName
94 << " (" << fieldPtrs.size() << " entries) for "
95 << coords_.size() << " sets" << nl
96 << exit(FatalError);
97 }
98
99 label nPoints = 0;
100 for (const auto& pts : coords_)
101 {
102 nPoints += pts.size();
103 }
104
105
106 // Field: rootdir/<TIME>/<field>_setName.csv
107
108 fileName outputFile = getFieldPrefixedPath(fieldName, "csv");
109
110 if (verbose_)
111 {
112 Info<< "Writing field " << fieldName;
113 Info<< " to " << outputFile << endl;
114 }
115
116 // Master only
117 {
118 if (!isDir(outputFile.path()))
119 {
120 mkDir(outputFile.path());
121 }
122
123 OFstream os(outputFile, streamOpt_);
124 os.precision(precision_);
125
126 // Header
127 {
129 writeHeader<Type>(os, fieldName);
130 os << nl;
131 }
132
133 forAll(coords_, tracki)
134 {
135 writeTable(os, coords_[tracki], fieldPtrs[tracki], ",");
136 }
137 }
138
139 wroteGeom_ = true;
140 return outputFile;
141}
142
143
144// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Generic templated field type.
Definition: Field.H:82
Output to file stream, using an OSstream.
Definition: OFstream.H:57
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:326
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
bool wroteGeom_
Track if geometry has been written since the last open.
static void writeTable(Ostream &os, const coordSet &coords, const UList< Type > &values, const char *sep)
Write coordinates and values.
bool verbose_
Additional output verbosity.
UPtrList< const coordSet > coords_
Reference to coordinate set(s)
fileName getFieldPrefixedPath(const word &fieldName, const word &fileExt=word::null) const
Get field-prefixed output file name.
Holds list of sampling positions.
Definition: coordSet.H:56
const word & axis() const
The sort axis name.
Definition: coordSet.H:140
bool hasVectorAxis() const noexcept
True if axis specification is a vector.
Definition: coordSet.C:135
A class for handling file names.
Definition: fileName.H:76
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
label nPoints
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: MSwindows.C:515
static void writeHeader(Ostream &os, const word &fieldName)
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
uint8_t direction
Definition: direction.H:56
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
static void writeCoordHeader(Ostream &os, const coordSet &coords, const label count)
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:651
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333