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-2021 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 
45 template<class Type>
47 :
48  writer<Type>(dict)
49 {}
50 
51 
52 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
53 
54 template<class Type>
56 (
57  const coordSet& points,
58  const wordList& valueSetNames
59 ) const
60 {
61  return this->getBaseName(points, valueSetNames) + ".vtk";
62 }
63 
64 
65 template<class Type>
67 (
68  const coordSet& points,
69  const wordList& valueSetNames,
70  const List<const Field<Type>*>& valueSets,
71  Ostream& os
72 ) const
73 {
74  os << "# vtk DataFile Version 2.0" << nl
75  << points.name() << nl
76  << "ASCII" << nl
77  << "DATASET POLYDATA" << nl
78  << "POINTS " << points.size() << " double" << nl;
79 
80  for (const point& pt : points)
81  {
82  os << float(pt.x()) << ' '
83  << float(pt.y()) << ' '
84  << float(pt.z()) << nl;
85  }
86 
87  os << "POINT_DATA " << points.size() << nl
88  << " FIELD attributes " << valueSetNames.size() << nl;
89 
90  forAll(valueSetNames, setI)
91  {
92  os << valueSetNames[setI] << ' '
93  << int(pTraits<Type>::nComponents) << ' '
94  << points.size() << " float" << nl;
95 
96  const Field<Type>& fld = *valueSets[setI];
97 
98  forAll(fld, pointi)
99  {
100  if (pointi)
101  {
102  os << ' ';
103  }
104  writer<Type>::write(fld[pointi], os);
105  }
106  os << nl;
107  }
108 }
109 
110 
111 template<class Type>
113 (
114  const bool writeTracks,
115  const List<scalarField>& times,
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 << " double" << nl;
142 
143  for (const coordSet& points : tracks)
144  {
145  for (const point& pt : points)
146  {
147  os << float(pt.x()) << ' '
148  << float(pt.y()) << ' '
149  << float(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  const label len = points.size();
164 
165  os << len;
166  for (label i = 0; i < len; ++i)
167  {
168  os << ' ' << globalPtI;
169  ++globalPtI;
170  }
171  os << nl;
172  }
173  }
174 
175  os << "POINT_DATA " << nPoints << nl
176  << " FIELD attributes " << valueSetNames.size() << nl;
177 
178  forAll(valueSetNames, setI)
179  {
180  os << valueSetNames[setI] << ' '
181  << int(pTraits<Type>::nComponents) << ' '
182  << nPoints << " float" << nl;
183 
184  const List<Field<Type>>& fieldVals = valueSets[setI];
185 
186  for (const Field<Type>& vals : fieldVals)
187  {
188  forAll(vals, j)
189  {
190  if (j)
191  {
192  os << ' ';
193  }
194  writer<Type>::write(vals[j], os);
195  }
196  os << nl;
197  }
198  }
199 }
200 
201 
202 // ************************************************************************* //
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
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:369
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:59
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
dict
dictionary dict
Definition: searchingEngine.H:14
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:67
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
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:81
Foam::coordSet
Holds list of sampling positions.
Definition: coordSet.H:53
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:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Vector< scalar >
Foam::vtkSetWriter::vtkSetWriter
vtkSetWriter()
Default construct.
Definition: vtkSetWriter.C:39
Foam::List< word >
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
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:56
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56