vtkSetWriter.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) 2016 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 "vtkSetWriter.H"
30 #include "coordSet.H"
31 #include "fileName.H"
32 #include "OFstream.H"
34 
35 
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37 
38 template<class Type>
40 :
41  writer<Type>()
42 {}
43 
44 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
45 
46 template<class Type>
48 {}
49 
50 
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 
53 template<class Type>
55 (
56  const coordSet& points,
57  const wordList& valueSetNames
58 ) const
59 {
60  return this->getBaseName(points, valueSetNames) + ".vtk";
61 }
62 
63 
64 template<class Type>
66 (
67  const coordSet& points,
68  const wordList& valueSetNames,
69  const List<const Field<Type>*>& valueSets,
70  Ostream& os
71 ) const
72 {
73  os << "# vtk DataFile Version 2.0" << nl
74  << points.name() << nl
75  << "ASCII" << nl
76  << "DATASET POLYDATA" << nl
77  << "POINTS " << points.size() << " double" << nl;
78 
79  for (const point& pt : points)
80  {
81  os << float(pt.x()) << ' '
82  << float(pt.y()) << ' '
83  << float(pt.z()) << nl;
84  }
85 
86  os << "POINT_DATA " << points.size() << nl
87  << " FIELD attributes " << valueSetNames.size() << nl;
88 
89  forAll(valueSetNames, setI)
90  {
91  os << valueSetNames[setI] << ' '
92  << int(pTraits<Type>::nComponents) << ' '
93  << points.size() << " float" << nl;
94 
95  const Field<Type>& fld = *valueSets[setI];
96 
97  forAll(fld, pointi)
98  {
99  if (pointi)
100  {
101  os << ' ';
102  }
103  writer<Type>::write(fld[pointi], os);
104  }
105  os << nl;
106  }
107 }
108 
109 
110 template<class Type>
112 (
113  const bool writeTracks,
114  const PtrList<coordSet>& tracks,
115  const wordList& valueSetNames,
116  const List<List<Field<Type>>>& valueSets,
117  Ostream& os
118 ) const
119 {
120  if (valueSets.size() != valueSetNames.size())
121  {
123  << "Number of variables:" << valueSetNames.size() << endl
124  << "Number of valueSets:" << valueSets.size()
125  << exit(FatalError);
126  }
127 
128  label nTracks = tracks.size();
129  label nPoints = 0;
130  forAll(tracks, i)
131  {
132  nPoints += tracks[i].size();
133  }
134 
135  os << "# vtk DataFile Version 2.0" << nl
136  << tracks[0].name() << nl
137  << "ASCII" << nl
138  << "DATASET POLYDATA" << nl
139  << "POINTS " << nPoints << " double" << nl;
140 
141  for (const coordSet& points : tracks)
142  {
143  for (const point& pt : points)
144  {
145  os << float(pt.x()) << ' '
146  << float(pt.y()) << ' '
147  << float(pt.z()) << nl;
148  }
149  }
150 
151  if (writeTracks)
152  {
153  os << "LINES " << nTracks << ' ' << nPoints+nTracks << nl;
154 
155  // Write ids of track points to file
156  label globalPtI = 0;
157  forAll(tracks, trackI)
158  {
159  const coordSet& points = tracks[trackI];
160 
161  const label len = points.size();
162 
163  os << len;
164  for (label i = 0; i < len; ++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] << ' '
179  << int(pTraits<Type>::nComponents) << ' '
180  << nPoints << " float" << nl;
181 
182  const List<Field<Type>>& fieldVals = valueSets[setI];
183 
184  for (const Field<Type>& vals : fieldVals)
185  {
186  forAll(vals, j)
187  {
188  if (j)
189  {
190  os << ' ';
191  }
192  writer<Type>::write(vals[j], os);
193  }
194  os << nl;
195  }
196  }
197 }
198 
199 
200 // ************************************************************************* //
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
coordSet.H
Foam::writer::write
virtual void write(const coordSet &, const wordList &, const List< const Field< Type > * > &, Ostream &) const =0
General entry point for writing.
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
OFstream.H
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::Field
Generic templated field type.
Definition: Field.H:63
fileName.H
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
Foam::vtkSetWriter::write
virtual void write(const coordSet &, const wordList &, const List< const Field< Type > * > &, Ostream &) const
General entry point for writing.
Definition: vtkSetWriter.C:66
Foam::FatalError
error FatalError
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::writer
Base class for graphics format writing. Entry points are.
Definition: writer.H:80
Foam::coordSet
Holds list of sampling positions.
Definition: coordSet.H:53
Foam::vtkSetWriter::~vtkSetWriter
virtual ~vtkSetWriter()
Destructor.
Definition: vtkSetWriter.C:47
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
vtkSetWriter.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::Vector< scalar >
Foam::vtkSetWriter::vtkSetWriter
vtkSetWriter()
Construct null.
Definition: vtkSetWriter.C:39
Foam::List< word >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:54
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::vtkSetWriter::getFileName
virtual fileName getFileName(const coordSet &, const wordList &) const
Generate file name with correct extension.
Definition: vtkSetWriter.C:55
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56