Loading...
Searching...
No Matches
rawCoordSetWriter.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2021-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "rawCoordSetWriter.H"
30#include "coordSet.H"
31#include "fileName.H"
32#include "OFstream.H"
33#include "OSspecific.H"
34#include "stringOps.H"
38// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40namespace Foam
41{
42namespace coordSetWriters
43{
47}
48}
49
50
51// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53// Implementation
55
56
57// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
58
60:
62 streamOpt_(),
63 precision_(IOstream::defaultPrecision())
64{
65 buffering_ = true;
66}
67
68
70:
71 coordSetWriter(options),
72 streamOpt_
73 (
74 IOstreamOption::ASCII,
75 IOstreamOption::compressionEnum("compression", options)
76 ),
77 precision_
78 (
79 options.getOrDefault("precision", IOstream::defaultPrecision())
80 )
81{
82 buffering_ = options.getOrDefault("buffer", true);
83}
84
85
87(
88 const coordSet& coords,
89 const fileName& outputPath,
90 const dictionary& options
91)
93 rawWriter(options)
94{
95 open(coords, outputPath);
96}
97
98
100(
101 const UPtrList<coordSet>& tracks,
102 const fileName& outputPath,
103 const dictionary& options
104)
105:
106 rawWriter(options)
108 open(tracks, outputPath);
109}
110
111
112// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
113
116 close();
117}
118
119
120// * * * * * * * * * * * * * * * * * Controls * * * * * * * * * * * * * * * //
121
123{
124 const bool old(buffering_);
126 return old;
127}
128
129
130// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
131
133{
134 // Assume !useTracks_, otherwise too fragile
135
136 // 1) rootdir/<TIME>/setName.raw
137 // 2) rootdir/setName.raw
138
139 return getExpectedPath("xy"); // Traditionally 'xy', not 'raw'
140}
141
142
144{
145 if (coords_.empty())
146 {
147 clearBuffers();
148 return false;
149 }
150 const auto& coords = coords_[0];
151
152 // Field:
153 // 1) rootdir/<TIME>/setName.raw
154 // 2) rootdir/setName.raw
155
156 fileName outputFile = path();
157
158 if (!isDir(outputFile.path()))
159 {
160 mkDir(outputFile.path());
161 }
162
163 OFstream os(outputFile, streamOpt_);
164 os.precision(precision_);
165
166 writeBufferContents(os, coords, " \t");
167
169
170 return true;
171}
172
173
174template<class Type>
175Foam::fileName Foam::coordSetWriters::rawWriter::writeTemplate
176(
177 const word& fieldName,
178 const Field<Type>& values
179)
180{
181 checkOpen();
182 if (coords_.empty())
183 {
184 return fileName::null;
185 }
186
187 if (useTracks_ || !buffering_)
188 {
189 UPtrList<const Field<Type>> fieldPtrs(repackageFields(values));
190 return writeTemplate(fieldName, fieldPtrs);
191 }
192
193 // Buffering version
194 appendField(fieldName, values);
195 return path();
196}
197
198
199template<class Type>
200Foam::fileName Foam::coordSetWriters::rawWriter::writeTemplate
201(
202 const word& fieldName,
203 const List<Field<Type>>& fieldValues
204)
205{
206 checkOpen();
207 if (coords_.empty())
208 {
209 return fileName::null;
210 }
211 useTracks_ = true; // Extra safety
212
213 UPtrList<const Field<Type>> fieldPtrs(repackageFields(fieldValues));
214 return writeTemplate(fieldName, fieldPtrs);
215}
216
217
218// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219
220// Field writing methods
221defineCoordSetWriterWriteFields(Foam::coordSetWriters::rawWriter);
222
223
224// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
A simple container for options an IOstream can normally have.
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition IOstream.H:85
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
Base class for writing coordSet(s) and tracks with fields.
void appendField(const word &fieldName, const Field< label > &vals)
virtual void open(const fileName &outputPath)
Write separate geometry to file.
fileName getExpectedPath(const word &fileExt=word::null) const
Get expected (characteristic) output file name - information only.
void writeBufferContents(Ostream &os, const coordSet &coords, const char *sep) const
Write buffered data.
bool buffering_
Writer with buffering output.
virtual bool buffering() const
True if the format uses internal buffering (eg, column output).
static UPtrList< const Field< Type > > repackageFields(const Field< Type > &field)
Repackage field into a UPtrList.
void checkOpen() const
Verify that the outputPath_ has been set or FatalError.
coordSetWriter(const coordSetWriter &)=delete
No copy construct.
bool useTracks_
Prefer tracks to points during single set writing.
void clearBuffers()
Clear out buffering.
UPtrList< const coordSet > coords_
Reference to coordinate set(s).
virtual void close(bool force=false)
Finish output, performing any necessary cleanup.
A coordSet(s) in raw format.
virtual ~rawWriter()
Destructor. Calls close().
virtual bool writeBuffered()
Write buffered data.
virtual fileName path() const
Characteristic output file name - information only.
Holds list of sampling positions.
Definition coordSet.H:52
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
A class for handling file names.
Definition fileName.H:75
static const fileName null
An empty fileName.
Definition fileName.H:111
static std::string path(const std::string &str)
Return directory path name (part before last /).
Definition fileNameI.H:169
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeName(Type)
Define the typeName.
Definition className.H:113
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Convenience macros for instantiating coordSetWriter methods.
#define defineCoordSetWriterWriteFields(ThisClass)
OBJstream os(runTime.globalPath()/outputName)
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 POSIX.C:616
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition POSIX.C:862