vtkSetWriter.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-2021 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 "vtkSetWriter.H"
27 #include "coordSet.H"
28 #include "fileName.H"
29 #include "OFstream.H"
31 
32 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
35 template<class Type>
37 :
38  setWriter<Type>()
39 {}
40 
41 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
42 
43 template<class Type>
45 {}
46 
47 
48 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
49 
50 template<class Type>
52 (
53  const coordSet& points,
54  const wordList& valueSetNames
55 ) const
56 {
57  return this->getBaseName(points, valueSetNames) + ".vtk";
58 }
59 
60 
61 template<class Type>
63 (
64  const coordSet& points,
65  const wordList& valueSetNames,
66  const List<const Field<Type>*>& valueSets,
67  Ostream& os
68 ) const
69 {
70  os << "# vtk DataFile Version 2.0" << nl
71  << points.name() << nl
72  << "ASCII" << nl
73  << "DATASET POLYDATA" << nl
74  << "POINTS " << points.size() << " float" << nl;
75 
76  forAll(points, i)
77  {
78  const vector& pt = points[i];
79  os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
80  }
81 
82  os << "VERTICES " << points.size() << ' ' << 2*points.size() << nl;
83 
84  forAll(points, i)
85  {
86  os << 1 << ' ' << i << endl;
87  }
88 
89  os << "POINT_DATA " << points.size() << nl
90  << " FIELD attributes " << valueSetNames.size() << nl;
91 
92  forAll(valueSetNames, setI)
93  {
94  os << valueSetNames[setI] << ' ' << pTraits<Type>::nComponents << ' '
95  << points.size() << " float" << nl;
96 
97  const Field<Type>& fld = *valueSets[setI];
98 
99  forAll(fld, pointi)
100  {
101  if (pointi != 0)
102  {
103  os << ' ';
104  }
105  setWriter<Type>::write(fld[pointi], os);
106  }
107  os << nl;
108  }
109 }
110 
111 
112 template<class Type>
114 (
115  const bool writeTracks,
116  const PtrList<coordSet>& tracks,
117  const wordList& valueSetNames,
118  const List<List<Field<Type>>>& valueSets,
119  Ostream& os
120 ) const
121 {
122  if (valueSets.size() != valueSetNames.size())
123  {
125  << "Number of variables:" << valueSetNames.size() << endl
126  << "Number of valueSets:" << valueSets.size()
127  << exit(FatalError);
128  }
129 
130  label nTracks = tracks.size();
131  label nPoints = 0;
132  forAll(tracks, i)
133  {
134  nPoints += tracks[i].size();
135  }
136 
137  os << "# vtk DataFile Version 2.0" << nl
138  << tracks[0].name() << nl
139  << "ASCII" << nl
140  << "DATASET POLYDATA" << nl
141  << "POINTS " << nPoints << " float" << nl;
142 
143  forAll(tracks, trackI)
144  {
145  const coordSet& points = tracks[trackI];
146  forAll(points, i)
147  {
148  const vector& pt = points[i];
149  os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
150  }
151  }
152 
153  if (writeTracks)
154  {
155  os << "LINES " << nTracks << ' ' << nPoints+nTracks << nl;
156 
157  // Write ids of track points to file
158  label globalPtI = 0;
159  forAll(tracks, trackI)
160  {
161  const coordSet& points = tracks[trackI];
162 
163  os << points.size();
164  forAll(points, i)
165  {
166  os << ' ' << globalPtI;
167  globalPtI++;
168  }
169  os << nl;
170  }
171  }
172 
173  os << "POINT_DATA " << nPoints << nl
174  << " FIELD attributes " << valueSetNames.size() << nl;
175 
176  forAll(valueSetNames, setI)
177  {
178  os << valueSetNames[setI] << ' ' << pTraits<Type>::nComponents << ' '
179  << nPoints << " float" << nl;
180 
181  const List<Field<Type>>& fieldVals = valueSets[setI];
182 
183  forAll(fieldVals, i)
184  {
185  const Field<Type>& vals = fieldVals[i];
186 
187  forAll(vals, j)
188  {
189  if (j != 0)
190  {
191  os << ' ';
192  }
193  setWriter<Type>::write(vals[j], os);
194  }
195  os << nl;
196  }
197  }
198 }
199 
200 
201 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
A class for handling file names.
Definition: fileName.H:79
virtual ~vtkSetWriter()
Destructor.
Definition: vtkSetWriter.C:44
virtual void write(const coordSet &, const wordList &, const List< const Field< Type > *> &, Ostream &) const =0
General entry point for writing.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
Base class for graphics format writing. Entry points are.
Definition: setWriter.H:79
virtual fileName getFileName(const coordSet &, const wordList &) const
Generate file name with correct extension.
Definition: vtkSetWriter.C:52
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
const Cmpt & z() const
Definition: VectorI.H:87
Macros for easy insertion into run-time selection tables.
const Cmpt & y() const
Definition: VectorI.H:81
vtkSetWriter()
Construct null.
Definition: vtkSetWriter.C:36
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){ const word &name=lagrangianScalarNames[i];IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Holds list of sampling positions.
Definition: coordSet.H:49
Pre-declare SubField and related Field type.
Definition: Field.H:56
label nPoints
const Cmpt & x() const
Definition: VectorI.H:75
virtual void write(const coordSet &, const wordList &, const List< const Field< Type > *> &, Ostream &) const
General entry point for writing.
Definition: vtkSetWriter.C:63
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
const word & name() const
Definition: coordSet.H:111
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
fileName getBaseName(const coordSet &, const wordList &) const
Generates filename from coordSet and sampled fields.
Definition: setWriter.C:60