setWriter.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-2023 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 "setWriter.H"
27 #include "coordSet.H"
28 #include "OFstream.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
38 }
39 
40 
41 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
42 
44 {
45  os << token::SPACE;
46 }
47 
48 
50 {
51  os << nl;
52 }
53 
54 
56 {
57  os << nl << nl;
58 }
59 
60 
62 {
63  if (!delimiters_.valid())
64  {
65  delimiters_.set(new List<string>(3));
66  OStringStream oss;
67  writeValueSeparator(oss);
68  delimiters_()[0] = oss.str();
69  oss.rewind();
70  writeCoordSeparator(oss);
71  delimiters_()[1] = oss.str();
72  oss.rewind();
73  writeSegmentSeparator(oss);
74  delimiters_()[2] = oss.str();
75  oss.rewind();
76  }
77 
78  return delimiters_;
79 }
80 
81 
83 (
84  const word& w,
85  Ostream& os,
86  const bool align,
87  const unsigned long alignPad
88 ) const
89 {
90  string s = w;
91  forAll(delimiters(), i)
92  {
93  if (w.find(delimiters()[i]) != string::npos)
94  {
95  s = "\"" + w + "\"";
96  break;
97  }
98  }
99 
100  if (!align)
101  {
102  os << s.c_str();
103  }
104  else if (s.size() < columnWidth(os) - alignPad)
105  {
106  os << string(columnWidth(os) - alignPad - s.size(), ' ').c_str()
107  << s.c_str();
108  }
109  else
110  {
111  os << s(columnWidth(os) - alignPad - 3).c_str() << "...";
112  }
113 
114  return os;
115 }
116 
117 
119 (
120  const coordSet& set,
121  const wordList& valueSetNames,
122  #define TypeValueSetsConstArg(Type, nullArg) \
123  const UPtrList<const Field<Type>>& Type##ValueSets ,
125  #undef TypeValueSetsConstArg
126  Ostream& os,
127  const bool align,
128  const unsigned long alignPad
129 ) const
130 {
131  bool first = true;
132 
133  // Write coordinate names
134  if (set.hasScalarAxis())
135  {
136  if (!first) writeValueSeparator(os);
137 
138  writeWord(set.scalarName(), os, align, first*alignPad);
139  first = false;
140  }
141  if (set.hasPointAxis())
142  {
143  for (direction cmpt = 0; cmpt < pTraits<point>::nComponents; ++ cmpt)
144  {
145  if (!first) writeValueSeparator(os);
146 
147  const bool separator =
148  !set.pointName().empty()
149  && strlen(pTraits<point>::componentNames[cmpt]) > 0;
150 
151  const word& w =
152  set.pointName()
153  + (separator ? "_" : "")
155 
156  writeWord(w, os, align, first*alignPad);
157  first = false;
158  }
159  }
160 
161  // Write value names
162  forAll(scalarValueSets, fieldi)
163  {
164  #define WriteTypeValueSetNames(Type, nullArg) \
165  if (Type##ValueSets.set(fieldi)) \
166  { \
167  const label nCmpt = pTraits<Type>::nComponents; \
168  \
169  for (direction cmpt = 0; cmpt < nCmpt; ++ cmpt) \
170  { \
171  if (!first) writeValueSeparator(os); \
172  \
173  const bool separator = \
174  !valueSetNames[fieldi].empty() \
175  && strlen(pTraits<Type>::componentNames[cmpt]) > 0; \
176  \
177  const word w = \
178  valueSetNames[fieldi] \
179  + (separator ? "_" : "") \
180  + pTraits<Type>::componentNames[cmpt]; \
181  \
182  writeWord(w, os, align, first*alignPad); \
183  first = false; \
184  } \
185  }
187  #undef WriteTypeValueSetNames
188  }
189 }
190 
191 
193 (
194  const coordSet& set,
195  #define TypeValueSetsConstArg(Type, nullArg) \
196  const UPtrList<const Field<Type>>& Type##ValueSets ,
198  #undef TypeValueSetsConstArg
199  Ostream& os,
200  const bool align
201 ) const
202 {
203  forAll(set, pointi)
204  {
205  if (pointi != 0)
206  {
207  writeCoordSeparator(os);
208 
209  if (set.segments()[pointi] != set.segments()[pointi - 1])
210  {
211  writeSegmentSeparator(os);
212  }
213  }
214 
215  bool first = true;
216 
217  if (set.hasScalarAxis())
218  {
219  if (!first) writeValueSeparator(os);
220  writeValue(set.scalarCoord(pointi), os, align);
221  first = false;
222  }
223  if (set.hasPointAxis())
224  {
225  if (!first) writeValueSeparator(os);
226  writeValue(set.pointCoord(pointi), os, align);
227  first = false;
228  }
229 
230  forAll(scalarValueSets, fieldi)
231  {
232  #define WriteTypeValueSets(Type, nullArg) \
233  if (Type##ValueSets.set(fieldi)) \
234  { \
235  if (!first) writeValueSeparator(os); \
236  writeValue(Type##ValueSets[fieldi][pointi], os, align); \
237  first = false; \
238  }
240  #undef WriteTypeValueSets
241  }
242  }
243 }
244 
245 
246 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
247 
249 (
250  const IOstream::streamFormat writeFormat,
251  const IOstream::compressionType writeCompression
252 )
253 :
254  writeFormat_(writeFormat),
255  writeCompression_(writeCompression)
256 {}
257 
258 
260 :
261  writeFormat_
262  (
263  dict.found("writeFormat")
264  ? IOstream::formatEnum(dict.lookup("writeFormat"))
265  : IOstream::ASCII
266  ),
267  writeCompression_
268  (
269  dict.found("writeCompression")
270  ? IOstream::compressionEnum(dict.lookup("writeCompression"))
271  : IOstream::UNCOMPRESSED
272  )
273 {}
274 
275 
277 :
278  writeFormat_(writer.writeFormat_),
279  writeCompression_(writer.writeCompression_)
280 {}
281 
282 
283 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
284 
286 (
287  const word& writeType,
288  const IOstream::streamFormat writeFormat,
289  const IOstream::compressionType writeCompression
290 )
291 {
292  typename wordConstructorTable::iterator cstrIter =
293  wordConstructorTablePtr_->find(writeType);
294 
295  if (cstrIter == wordConstructorTablePtr_->end())
296  {
298  << "Unknown write type "
299  << writeType << nl << nl
300  << "Valid write types : " << endl
301  << wordConstructorTablePtr_->sortedToc()
302  << exit(FatalError);
303  }
304 
305  return autoPtr<setWriter>(cstrIter()(writeFormat, writeCompression));
306 }
307 
308 
310 (
311  const word& writeType,
312  const dictionary& dict
313 )
314 {
315  // find constructors with dictionary options
316  dictConstructorTable::iterator cstrIter =
317  dictConstructorTablePtr_->find(writeType);
318 
319  if (cstrIter == dictConstructorTablePtr_->end())
320  {
321  const IOstream::streamFormat writeFormat =
322  dict.found("writeFormat")
323  ? IOstream::formatEnum(dict.lookup("writeFormat"))
324  : IOstream::ASCII;
325 
326  const IOstream::compressionType writeCompression =
327  dict.found("writeCompression")
328  ? IOstream::compressionEnum(dict.lookup("writeCompression"))
330 
331  // Revert to versions without options
332  return
334  (
335  writeType,
336  writeFormat,
337  writeCompression
338  );
339  }
340 
341  return autoPtr<setWriter>(cstrIter()(dict));
342 }
343 
344 
345 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
346 
348 {}
349 
350 
351 // ************************************************************************* //
bool found
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Macros for easy insertion into run-time selection tables.
Pre-declare SubField and related Field type.
Definition: Field.H:82
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:72
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
static compressionType compressionEnum(const word &)
Return compression of given compression name.
Definition: IOstream.C:61
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
Output to memory buffer stream.
Definition: OStringStream.H:52
void rewind()
Rewind the OStringStream.
string str() const
Return the string.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:66
Holds list of sampling positions.
Definition: coordSet.H:51
bool hasPointAxis() const
Is the coordinate axis a point?
Definition: coordSet.C:152
point pointCoord(const label index) const
Get vector coordinate (axis is xyz)
Definition: coordSet.C:254
word scalarName() const
Return the name of the scalar coordinates.
Definition: coordSet.C:223
const labelList & segments() const
Return the segments.
Definition: coordSet.H:141
word pointName() const
Return the name of the point coordinates.
Definition: coordSet.C:308
bool hasScalarAxis() const
Is the coordinate axis a scalar?
Definition: coordSet.C:138
scalar scalarCoord(const label index) const
Get scalar coordinate (axis is x, y, z or distance)
Definition: coordSet.C:163
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:860
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:659
Traits class for primitives.
Definition: pTraits.H:53
Base class for writing coordinate sets with data.
Definition: setWriter.H:64
void writeTable(const coordSet &set, #define TypeValueSetsConstArg(Type, nullArg) Ostream &os, const bool align=false) const
Write multi-column table of data.
Definition: setWriter.C:193
virtual ~setWriter()=0
Destructor.
Definition: setWriter.C:347
const List< string > & delimiters() const
Possible delimiters that need quoting.
Definition: setWriter.C:61
virtual void writeSegmentSeparator(Ostream &os) const
Write a segment separator.
Definition: setWriter.C:55
void writeTableHeader(const coordSet &set, const wordList &valueSetNames, #define TypeValueSetsConstArg(Type, nullArg) Ostream &os, const bool align=false, const unsigned long alignPad=0) const
Write multi-column table header.
Definition: setWriter.C:119
static autoPtr< setWriter > New(const word &writeType, const IOstream::streamFormat writeFormat=IOstream::ASCII, const IOstream::compressionType writeCompression=IOstream::UNCOMPRESSED)
Select given write options.
Definition: setWriter.C:286
virtual void writeCoordSeparator(Ostream &os) const
Write a coordinate separator.
Definition: setWriter.C:49
Ostream & writeWord(const word &w, Ostream &os, const bool align=false, const unsigned long alignPad=0) const
Write a word.
Definition: setWriter.C:83
virtual void writeValueSeparator(Ostream &os) const
Write a value separator.
Definition: setWriter.C:43
setWriter(const IOstream::streamFormat writeFormat, const IOstream::compressionType writeCompression)
Construct given write options.
Definition: setWriter.C:249
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
FOR_ALL_FIELD_TYPES(DefineContiguousFvWallLocationDataType)
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
defineTypeNameAndDebug(combustionModel, 0)
error FatalError
static const char nl
Definition: Ostream.H:260
uint8_t direction
Definition: direction.H:45
dictionary dict
#define WriteTypeValueSets(Type, nullArg)
#define WriteTypeValueSetNames(Type, nullArg)
#define TypeValueSetsConstArg(Type, nullArg)