writer.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) 2019-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 "writer.H"
30 #include "coordSet.H"
31 #include "OFstream.H"
32 #include "OSspecific.H"
33 
34 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
35 
36 template<class Type>
38 (
39  const word& writeType
40 )
41 {
42  auto* ctorPtr = wordConstructorTable(writeType);
43 
44  if (!ctorPtr)
45  {
47  (
48  "writer",
49  writeType,
50  *wordConstructorTablePtr_
51  ) << exit(FatalError);
52  }
53 
54  return autoPtr<writer<Type>>(ctorPtr());
55 }
56 
57 
58 template<class Type>
60 (
61  const word& writeType,
63 )
64 {
65  auto* ctorPtr = dictConstructorTable(writeType);
66 
67  if (!ctorPtr)
68  {
70  (
71  "writer",
72  writeType,
73  *dictConstructorTablePtr_
74  ) << exit(FatalError);
75  }
76 
77  return autoPtr<writer<Type>>(ctorPtr(formatOptions));
78 }
79 
80 
81 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
82 
83 template<class Type>
85 (
86  const coordSet& points,
87  const wordList& valueSets
88 ) const
89 {
90  fileName fName(points.name());
91 
92  forAll(valueSets, i)
93  {
94  fName += '_' + valueSets[i];
95  }
96 
97  return fName;
98 }
99 
100 
101 template<class Type>
103 (
104  const coordSet& points,
105  const label pointi,
106  Ostream& os
107 ) const
108 {
109  if (points.hasVectorAxis())
110  {
111  write(points.vectorCoord(pointi), os);
112  }
113  else
114  {
115  write(points.scalarCoord(pointi), os);
116  }
117 }
118 
119 
120 template<class Type>
122 (
123  const coordSet& points,
124  const List<Type>& values,
125  Ostream& os
126 ) const
127 {
128  forAll(points, pointi)
129  {
130  writeCoord(points, pointi, os);
131  writeSeparator(os);
132  write(values[pointi], os);
133  os << nl;
134  }
135 }
136 
137 
138 template<class Type>
140 (
141  const coordSet& points,
142  const List<const List<Type>*>& valuesPtrList,
143  Ostream& os
144 ) const
145 {
146  forAll(points, pointi)
147  {
148  writeCoord(points, pointi, os);
149 
150  forAll(valuesPtrList, i)
151  {
152  writeSeparator(os);
153 
154  const List<Type>& values = *valuesPtrList[i];
155  write(values[pointi], os);
156  }
157  os << nl;
158  }
159 }
160 
161 
162 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
163 
164 template<class Type>
166 {}
167 
168 
169 template<class Type>
171 {}
172 
173 
174 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
175 
176 template<class Type>
178 (
179  const coordSet& points,
180  const wordList& valueSetNames,
181  const List<Field<Type>>& valueSets,
182  Ostream& os
183 ) const
184 {
185  List<const Field<Type>*> valueSetPtrs(valueSets.size());
186  forAll(valueSetPtrs, i)
187  {
188  valueSetPtrs[i] = &valueSets[i];
189  }
190  write(points, valueSetNames, valueSetPtrs, os);
191 }
192 
193 
194 template<class Type>
196 (
197  const scalar value,
198  Ostream& os
199 ) const
200 {
201  return os << value;
202 }
203 
204 
205 template<class Type>
206 template<class VSType>
208 (
209  const VSType& value,
210  Ostream& os
211 ) const
212 {
213  for (direction d=0; d<VSType::nComponents; d++)
214  {
215  if (d > 0)
216  {
217  writeSeparator(os);
218  }
219 
220  os << value.component(d);
221  }
222  return os;
223 }
224 
225 
226 template<class Type>
228 (
229  Ostream& os
230 ) const
231 {
232  os << token::SPACE << token::TAB;
233 }
234 
235 
236 template<class Type>
238 (
239  const vector& value,
240  Ostream& os
241 ) const
242 {
243  return writeVS(value, os);
244 }
245 
246 
247 template<class Type>
249 (
250  const sphericalTensor& value,
251  Ostream& os
252 ) const
253 {
254  return writeVS(value, os);
255 }
256 
257 
258 template<class Type>
260 (
261  const symmTensor& value,
262  Ostream& os
263 ) const
264 {
265  return writeVS(value, os);
266 }
267 
268 
269 template<class Type>
271 (
272  const tensor& value,
273  Ostream& os
274 ) const
275 {
276  return writeVS(value, os);
277 }
278 
279 
280 // ************************************************************************* //
Foam::Tensor< scalar >
Foam::SymmTensor< scalar >
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::writer::writeVS
Ostream & writeVS(const VSType &, Ostream &) const
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
coordSet.H
Foam::writer::getBaseName
fileName getBaseName(const coordSet &, const wordList &) const
Generates filename from coordSet and sampled fields.
Definition: writer.C:85
Foam::writer::write
virtual void write(const coordSet &, const wordList &, const List< const Field< Type > * > &, Ostream &) const =0
General entry point for writing.
formatOptions
const dictionary formatOptions
Definition: createFields.H:26
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
OFstream.H
Foam::Field
Generic templated field type.
Definition: Field.H:63
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
FatalErrorInLookup
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:457
os
OBJstream os(runTime.globalPath()/outputName)
Foam::coordSet
Holds list of sampling positions.
Definition: coordSet.H:53
Foam::SphericalTensor< scalar >
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::writer::writer
writer()
Default construct.
Definition: writer.C:165
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::writer::writeTable
void writeTable(const coordSet &, const List< Type > &, Ostream &) const
Writes single-column ascii write. Column 1 is coordSet coordinate,.
Definition: writer.C:122
Foam::Vector< scalar >
Foam::List< word >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::writer::writeCoord
void writeCoord(const coordSet &, const label sampleI, Ostream &) const
Definition: writer.C:103
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
writer.H
Foam::writer::writeSeparator
virtual void writeSeparator(Ostream &os) const
Writes a separator. Used by write functions.
Definition: writer.C:228
Foam::writer::New
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: writer.C:38