foamVtkLagrangianWriterTemplates.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) 2016-2019 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 
29 #include "IOobjectList.H"
30 #include "IOField.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 template<class Type>
36 {
38  {
39  ++nCellData_;
40  }
42  {
43  ++nPointData_;
44  }
45  else
46  {
48  << "Bad writer state (" << stateNames[state_]
49  << ") - should be (" << stateNames[outputState::CELL_DATA]
50  << ") or (" << stateNames[outputState::POINT_DATA]
51  << ") for field " << field.name() << nl << endl
52  << exit(FatalError);
53  }
54 
55  static_assert
56  (
57  (
58  std::is_same<label, typename pTraits<Type>::cmptType>::value
59  || std::is_floating_point<typename pTraits<Type>::cmptType>::value
60  ),
61  "Label and Floating-point vector space only"
62  );
63 
64 
65  // Other integral types (eg, bool etc) would need cast/convert to label.
66  // Similarly for labelVector etc.
67 
68  // // Ensure consistent output width
69  // for (const Type& val : field)
70  // {
71  // for (int cmpt=0; cmpt < nCmpt; ++cmpt)
72  // {
73  // format().write(label(component(val, cmpt)));
74  // }
75  // }
76 
77 
79 
80  label nVals = field.size();
81 
82  if (parallel_)
83  {
84  reduce(nVals, sumOp<label>());
85  }
86 
87  if (format_)
88  {
89  // Non-legacy
90 
91  if (std::is_same<label, typename pTraits<Type>::cmptType>::value)
92  {
93  const uint64_t payLoad = vtk::sizeofData<label, nCmpt>(nVals);
94 
95  format().beginDataArray<label, nCmpt>(field.name());
96  format().writeSize(payLoad);
97  }
98  else
99  {
100  const uint64_t payLoad = vtk::sizeofData<float, nCmpt>(nVals);
101 
102  format().beginDataArray<float, nCmpt>(field.name());
103  format().writeSize(payLoad);
104  }
105  }
106 
107  if (parallel_)
108  {
110  }
111  else
112  {
114  }
115 
116 
117  if (format_)
118  {
119  format().flush();
120  format().endDataArray();
121  }
122 }
123 
124 
125 template<class Type>
127 (
128  const wordList& fieldNames,
129  bool verbose
130 )
131 {
132  const fileName localDir(cloudDir());
133 
134  label nFields = 0;
135 
136  for (const word& fieldName : fieldNames)
137  {
138  // Globally the field is expected to exist (MUST_READ), but can
139  // be missing on a local processor.
140  //
141  // However, constructing IOField with MUST_READ and valid=false fails.
142  // Workaround: READ_IF_PRESENT and verify the header globally
143 
144  IOobject io
145  (
146  fieldName,
147  mesh_.time().timeName(),
148  localDir,
149  mesh_,
151  );
152 
153  // Check global existence to avoid any errors
154  const bool ok =
156  (
157  io.typeHeaderOk<IOField<Type>>(false),
158  orOp<bool>()
159  );
160 
161  if (!ok)
162  {
163  continue;
164  }
165 
166  if (verbose)
167  {
168  if (!nFields)
169  {
170  Info<< " " << pTraits<Type>::typeName << ":";
171  }
172 
173  Info<< " " << fieldName;
174  }
175 
176  IOField<Type> field(io);
177  this->write<Type>(field);
178 
179  ++nFields;
180  }
181 
182  if (verbose && nFields)
183  {
184  Info << endl;
185  }
186 
187  return nFields;
188 }
189 
190 
191 template<class Type>
193 (
194  const IOobjectList& objects,
195  const bool verbose
196 )
197 {
198  return writeFields<Type>
199  (
200  objects.allNames<IOField<Type>>(), // These are in sorted order
201  verbose
202  );
203 }
204 
205 
206 // ************************************************************************* //
Foam::vtk::writeListParallel
void writeListParallel(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
Definition: foamVtkOutput.C:126
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::vtk::formatter::endDataArray
virtual formatter & endDataArray()
End "DataArray" XML section.
Definition: foamVtkFormatterI.H:294
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
Foam::vtk::formatter::writeSize
virtual bool writeSize(const uint64_t numbytes)=0
Write leading size for binary output.
Foam::vtk::lagrangianWriter::writeFields
label writeFields(const wordList &fieldNames, bool verbose=true)
Write IOFields.
IOobjectList.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
foamVtkLagrangianWriter.H
Foam::vtk::fileWriter::stateNames
static const Enum< outputState > stateNames
Names for the output state (for messages, not for file output).
Definition: foamVtkFileWriter.H:85
Foam::vtk::fileWriter::format_
autoPtr< vtk::formatter > format_
The VTK formatter in use (master process)
Definition: foamVtkFileWriter.H:110
Foam::vtk::fileWriter::nPointData_
label nPointData_
The number of PointData written for the Piece thus far.
Definition: foamVtkFileWriter.H:104
Foam::sumOp
Definition: ops.H:213
Foam::vtk::formatter::beginDataArray
formatter & beginDataArray(const word &dataName, uint64_t payLoad=npos, bool leaveOpen=false)
Begin "DataArray" XML section.
Foam::vtk::fileWriter::state_
outputState state_
The output state.
Definition: foamVtkFileWriter.H:98
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:51
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::vtk::lagrangianWriter::write
void write(const IOField< Type > &field)
Write the IOField.
Definition: foamVtkLagrangianWriterTemplates.C:35
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:122
field
rDeltaTY field()
Foam::vtk::writeList
void writeList(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
Definition: foamVtkOutput.C:112
Foam::vtk::fileWriter::parallel_
bool parallel_
Writing in parallel (via master)
Definition: foamVtkFileWriter.H:95
Foam::FatalError
error FatalError
Foam::vtk::formatter::flush
virtual void flush()=0
Flush encoding, write newline etc.
Foam::IOobjectList::allNames
wordList allNames() const
The sorted names of all objects (synchronised across processors)
Definition: IOobjectList.C:391
Foam::IOobjectList
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:55
Foam::vtk::fileTag::POINT_DATA
"PointData"
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::nl
constexpr char nl
Definition: Ostream.H:385
IOField.H
Foam::List< word >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:54
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::vtk::fileTag::CELL_DATA
"CellData"
Foam::vtk::fileWriter::nCellData_
label nCellData_
The number of CellData written for the Piece thus far.
Definition: foamVtkFileWriter.H:101
Foam::vtk::fileWriter::format
vtk::formatter & format()
The VTK formatter in use.
Definition: foamVtkFileWriterI.H:36
Foam::vtk::fileWriter::isState
bool isState(outputState test) const
True if the output state corresponds to the test state.
Definition: foamVtkFileWriterI.H:42
Foam::orOp
Definition: ops.H:234