All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2018 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  writer<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 
83  os << "POINT_DATA " << points.size() << nl
84  << " FIELD attributes " << valueSetNames.size() << nl;
85 
86  forAll(valueSetNames, setI)
87  {
88  os << valueSetNames[setI] << ' ' << pTraits<Type>::nComponents << ' '
89  << points.size() << " float" << nl;
90 
91  const Field<Type>& fld = *valueSets[setI];
92 
93  forAll(fld, pointi)
94  {
95  if (pointi != 0)
96  {
97  os << ' ';
98  }
99  writer<Type>::write(fld[pointi], os);
100  }
101  os << nl;
102  }
103 }
104 
105 
106 template<class Type>
108 (
109  const bool writeTracks,
110  const PtrList<coordSet>& tracks,
111  const wordList& valueSetNames,
112  const List<List<Field<Type>>>& valueSets,
113  Ostream& os
114 ) const
115 {
116  if (valueSets.size() != valueSetNames.size())
117  {
119  << "Number of variables:" << valueSetNames.size() << endl
120  << "Number of valueSets:" << valueSets.size()
121  << exit(FatalError);
122  }
123 
124  label nTracks = tracks.size();
125  label nPoints = 0;
126  forAll(tracks, i)
127  {
128  nPoints += tracks[i].size();
129  }
130 
131  os << "# vtk DataFile Version 2.0" << nl
132  << tracks[0].name() << nl
133  << "ASCII" << nl
134  << "DATASET POLYDATA" << nl
135  << "POINTS " << nPoints << " float" << nl;
136 
137  forAll(tracks, trackI)
138  {
139  const coordSet& points = tracks[trackI];
140  forAll(points, i)
141  {
142  const vector& pt = points[i];
143  os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
144  }
145  }
146 
147  if (writeTracks)
148  {
149  os << "LINES " << nTracks << ' ' << nPoints+nTracks << nl;
150 
151  // Write ids of track points to file
152  label globalPtI = 0;
153  forAll(tracks, trackI)
154  {
155  const coordSet& points = tracks[trackI];
156 
157  os << points.size();
158  forAll(points, i)
159  {
160  os << ' ' << globalPtI;
161  globalPtI++;
162  }
163  os << nl;
164  }
165  }
166 
167  os << "POINT_DATA " << nPoints << nl
168  << " FIELD attributes " << valueSetNames.size() << nl;
169 
170  forAll(valueSetNames, setI)
171  {
172  os << valueSetNames[setI] << ' ' << pTraits<Type>::nComponents << ' '
173  << nPoints << " float" << nl;
174 
175  const List<Field<Type>>& fieldVals = valueSets[setI];
176 
177  forAll(fieldVals, i)
178  {
179  const Field<Type>& vals = fieldVals[i];
180 
181  forAll(vals, j)
182  {
183  if (j != 0)
184  {
185  os << ' ';
186  }
187  writer<Type>::write(vals[j], os);
188  }
189  os << nl;
190  }
191  }
192 }
193 
194 
195 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
fileName getBaseName(const coordSet &, const wordList &) const
Generates filename from coordSet and sampled fields.
Definition: writer.C:60
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
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:319
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
virtual void write(const coordSet &, const wordList &, const List< const Field< Type > *> &, Ostream &) const =0
General entry point for writing.
Base class for graphics format writing. Entry points are.
Definition: writer.H:78
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