rawCoordSetWriterImpl.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 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  // Output coordinate header
37  static void writeCoordHeader
38  (
39  Ostream& os,
40  const coordSet& coords,
41  const label count
42  )
43  {
44  if (coords.hasVectorAxis())
45  {
46  os << "POINT_DATA" << ' ' << count << nl;
47  os << "# x y z";
48  }
49  else
50  {
51  word axisName(coords.axis());
52  word dataName(stringOps::upper(axisName) + "_DATA");
53 
54  os << dataName << ' ' << count << nl;
55  os << "# " << axisName;
56  }
57  }
58 
59  // Write field name, use named components for VectorSpace
60  template<class Type>
61  static inline void writeHeader(Ostream& os, const word& fieldName)
62  {
63  os << ' '; // Extra space
64 
65  const auto nCmpts(pTraits<Type>::nComponents);
66 
67  if (pTraits<Type>::rank || nCmpts > 1)
68  {
69  for (direction d = 0; d < nCmpts; ++d)
70  {
71  os << ' ' << fieldName
72  << '_' << pTraits<Type>::componentNames[d];
73  }
74  }
75  else
76  {
77  os << ' ' << fieldName;
78  }
79  }
80 
81 } // End namespace Foam
82 
83 
84 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
85 
86 template<class Type>
87 Foam::fileName Foam::coordSetWriters::rawWriter::writeTemplate
88 (
89  const word& fieldName,
90  const UPtrList<const Field<Type>>& fieldPtrs
91 )
92 {
93  if (coords_.size() != fieldPtrs.size())
94  {
96  << "Attempted to write field: " << fieldName
97  << " (" << fieldPtrs.size() << " entries) for "
98  << coords_.size() << " sets" << nl
99  << exit(FatalError);
100  }
101 
102  label nPoints = 0;
103  for (const auto& pts : coords_)
104  {
105  nPoints += pts.size();
106  }
107 
108 
109  // Field: rootdir/<TIME>/<field>_setName.raw
110 
111  fileName outputFile = getFieldPrefixedPath(fieldName, "raw");
112 
113  if (verbose_)
114  {
115  Info<< "Writing field " << fieldName;
116  Info<< " to " << outputFile << endl;
117  }
118 
119  // Master only
120  {
121  if (!isDir(outputFile.path()))
122  {
123  mkDir(outputFile.path());
124  }
125 
126  OFstream os(outputFile, streamOpt_);
127  os.precision(precision_);
128 
129  // Header
130  {
131  os << "# " << fieldName << " ";
132 
134  writeHeader<Type>(os, fieldName);
135  os << nl;
136  }
137 
138  forAll(coords_, tracki)
139  {
140  writeTable(os, coords_[tracki], fieldPtrs[tracki], " ");
141  }
142  }
143 
144  wroteGeom_ = true;
145  return outputFile;
146 }
147 
148 
149 // ************************************************************************* //
uint8_t direction
Definition: direction.H:46
A class for handling file names.
Definition: fileName.H:72
static void writeHeader(Ostream &os, const word &fieldName)
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
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
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1187
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
bool hasVectorAxis() const noexcept
True if axis specification is a vector.
Definition: coordSet.C:128
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...
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
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
label nPoints
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
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.
static void writeCoordHeader(Ostream &os, const coordSet &coords, const label count)
::Foam::direction rank(const expressions::valueTypeCode) noexcept
The vector-space rank associated with given valueTypeCode.
Definition: exprTraits.C:70
messageStream Info
Information stream (stdout output on master, null elsewhere)
Namespace for OpenFOAM.
const pointField & pts