writeVTK.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) 2016-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 "writeVTK.H"
27 #include "Time.H"
28 #include "vtkMesh.H"
29 #include "internalWriter.H"
30 #include "OSspecific.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace functionObjects
38 {
40  addToRunTimeSelectionTable(functionObject, writeVTK, dictionary);
41 }
42 }
43 
44 
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46 
48 (
49  const word& name,
50  const Time& runTime,
51  const dictionary& dict
52 )
53 :
54  fvMeshFunctionObject(name, runTime, dict),
55  objectNames_()
56 {
57  read(dict);
58 }
59 
60 
61 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
62 
64 {}
65 
66 
67 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
68 
69 bool Foam::functionObjects::writeVTK::read(const dictionary& dict)
70 {
71  dict.lookup("objects") >> objectNames_;
72 
73  return true;
74 }
75 
76 
78 {
79  return objectNames_;
80 }
81 
82 
84 {
85  return true;
86 }
87 
88 
90 {
91  Info<< type() << " " << name() << " output:" << nl;
92 
93  Info<< "Time: " << time_.name() << endl;
94 
95  word timeDesc = time_.name();
96 
97  // VTK/ directory in the case
98  fileName fvPath(time_.path()/"VTK");
99 
100  mkDir(fvPath);
101 
102  string vtkName = time_.caseName();
103 
104  if (Pstream::parRun())
105  {
106  // Strip off leading casename, leaving just processor_DDD ending.
107  string::size_type i = vtkName.rfind("processor");
108 
109  if (i != string::npos)
110  {
111  vtkName = vtkName.substr(i);
112  }
113  }
114 
115  // Create file and write header
116  fileName vtkFileName
117  (
118  fvPath/vtkName
119  + "_"
120  + timeDesc
121  + ".vtk"
122  );
123 
124  Info<< " Internal : " << vtkFileName << endl;
125 
126  vtkMesh vMesh(const_cast<fvMesh&>(mesh_));
127 
128  // Write mesh
129  internalWriter writer(vMesh, false, vtkFileName);
130 
131  // Declare UPtrLists to the volFields that are to be written
132  #define DeclareTypeFields(Type, nullArg) \
133  UPtrList<const VolField<Type>> Type##Fields;
135  #undef DeclareTypeFields
136 
137  // Look up the volFields and store the pointers
138  label nFields = 0;
139  forAll(objectNames_, i)
140  {
141  bool objectFound = false;
142 
143  #define SetTypeFields(Type, nullarg) \
144  { \
145  if (obr_.foundObject<VolField<Type>>(objectNames_[i])) \
146  { \
147  const VolField<Type>& field = \
148  obr_.lookupObject<VolField<Type>>(objectNames_[i]); \
149  \
150  Type##Fields.resize(Type##Fields.size() + 1); \
151  Type##Fields.set(Type##Fields.size() - 1, &field); \
152  \
153  Info<< " Writing " << VolField<Type>::typeName \
154  << " field " << field.name() << endl; \
155  \
156  nFields ++; \
157  objectFound = true; \
158  } \
159  }
160  FOR_ALL_FIELD_TYPES(SetTypeFields);
161  #undef SetTypeFields
162 
163  if (!objectFound)
164  {
165  cannotFindObject(objectNames_[i]);
166  }
167  }
168 
169  // Write header for cellID and volFields
171  (
172  writer.os(),
173  vMesh.nFieldCells(),
174  1 + nFields
175  );
176 
177  // Write cellID field
178  writer.writeCellIDs();
179 
180  // Write volFields
181  #define WriteTypeFields(Type, nullArg) \
182  writer.write(Type##Fields);
183  FOR_ALL_FIELD_TYPES(WriteTypeFields);
184  #undef WriteTypeFields
185 
186  return true;
187 }
188 
189 
190 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Macros for easy insertion into run-time selection tables.
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
virtual wordList fields() const
Return the list of fields required.
virtual bool execute()
Execute, currently does nothing.
virtual ~writeVTK()
Destructor.
virtual bool write()
Write the writeVTK.
virtual bool read(const dictionary &)
Read the writeVTK data.
writeVTK(const word &name, const Time &t, const dictionary &)
Construct from Time and dictionary.
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
defineTypeNameAndDebug(adjustTimeStepToCombustion, 0)
addToRunTimeSelectionTable(functionObject, adjustTimeStepToCombustion, dictionary)
void writeCellDataHeader(std::ostream &, const label nCells, const label nFields)
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
messageStream Info
FOR_ALL_FIELD_TYPES(DefineContiguousFvWallLocationDataType)
void writeVTK(OFstream &os, const Type &value)
static const char nl
Definition: Ostream.H:260
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
#define DeclareTypeFields(Type, nullArg)
dictionary dict