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-2022 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 {
35  defineTypeNameAndDebug(setWriter, 0);
36  defineRunTimeSelectionTable(setWriter, word);
37  defineRunTimeSelectionTable(setWriter, dict);
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 
276 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
277 
279 (
280  const word& writeType,
281  const IOstream::streamFormat writeFormat,
282  const IOstream::compressionType writeCompression
283 )
284 {
285  typename wordConstructorTable::iterator cstrIter =
286  wordConstructorTablePtr_->find(writeType);
287 
288  if (cstrIter == wordConstructorTablePtr_->end())
289  {
291  << "Unknown write type "
292  << writeType << nl << nl
293  << "Valid write types : " << endl
294  << wordConstructorTablePtr_->sortedToc()
295  << exit(FatalError);
296  }
297 
298  return autoPtr<setWriter>(cstrIter()(writeFormat, writeCompression));
299 }
300 
301 
303 (
304  const word& writeType,
305  const dictionary& dict
306 )
307 {
308  // find constructors with dictionary options
309  dictConstructorTable::iterator cstrIter =
310  dictConstructorTablePtr_->find(writeType);
311 
312  if (cstrIter == dictConstructorTablePtr_->end())
313  {
314  const IOstream::streamFormat writeFormat =
315  dict.found("writeFormat")
316  ? IOstream::formatEnum(dict.lookup("writeFormat"))
317  : IOstream::ASCII;
318 
319  const IOstream::compressionType writeCompression =
320  dict.found("writeCompression")
321  ? IOstream::compressionEnum(dict.lookup("writeCompression"))
323 
324  // Revert to versions without options
325  return
327  (
328  writeType,
329  writeFormat,
330  writeCompression
331  );
332  }
333 
334  return autoPtr<setWriter>(cstrIter()(dict));
335 }
336 
337 
338 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
339 
341 {}
342 
343 
344 // ************************************************************************* //
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:279
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:38
dictionary dict
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
static compressionType compressionEnum(const word &)
Return compression of given compression name.
Definition: IOstream.C:61
Ostream & writeWord(const word &w, Ostream &os, const bool align=false, const unsigned long alignPad=0) const
Write a word.
Definition: setWriter.C:83
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
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
uint8_t direction
Definition: direction.H:45
const List< string > & delimiters() const
Possible delimiters that need quoting.
Definition: setWriter.C:61
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Traits class for primitives.
Definition: pTraits.H:50
#define TypeValueSetsConstArg(Type, nullArg)
void rewind()
Rewind the OStringStream.
virtual void writeValueSeparator(Ostream &os) const
Write a value separator.
Definition: setWriter.C:43
virtual ~setWriter()=0
Destructor.
Definition: setWriter.C:340
Macros for easy insertion into run-time selection tables.
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
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.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
stressControl lookup("compactNormalStress") >> compactNormalStress
Holds list of sampling positions.
Definition: coordSet.H:50
static word scalarName
Definition: globalFoam.H:32
Pre-declare SubField and related Field type.
Definition: Field.H:56
A class for handling words, derived from string.
Definition: word.H:59
virtual void writeSegmentSeparator(Ostream &os) const
Write a segment separator.
Definition: setWriter.C:55
#define WriteTypeValueSets(Type, nullArg)
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:54
virtual void writeCoordSeparator(Ostream &os) const
Write a coordinate separator.
Definition: setWriter.C:49
setWriter(const IOstream::streamFormat writeFormat, const IOstream::compressionType writeCompression)
Construct given write options.
Definition: setWriter.C:249
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
defineTypeNameAndDebug(combustionModel, 0)
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:71
string str() const
Return the string.
#define WriteTypeValueSetNames(Type, nullArg)
FOR_ALL_FIELD_TYPES(DefineFvWallInfoType)
A class for handling character strings derived from std::string.
Definition: string.H:76
bool found
Output to memory buffer stream.
Definition: OStringStream.H:49
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864