CloudToVTK.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 | Copyright (C) 2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 "CloudToVTK.H"
27 #include "vtkTools.H"
28 #include "floatScalar.H"
29 
30 // * * * * * * * * * * * * * Protectd Member Functions * * * * * * * * * * * //
31 
32 template<class CloudType>
34 (
35  std::ostream& vtkOs,
36  const bool binary,
37  const List<floatScalar>& data
38 ) const
39 {
40  const label procI = Pstream::myProcNo();
41 
42  List<List<floatScalar> > allProcData(Pstream::nProcs());
43  allProcData[procI] = data;
44  Pstream::gatherList(allProcData);
45  List<floatScalar> allData =
46  ListListOps::combine<List<floatScalar> >
47  (
48  allProcData,
50  );
51 
52  vtkTools::write(vtkOs, binary, allData);
53 }
54 
55 
56 template<class CloudType>
57 template<class Type>
59 (
60  std::ostream& vtkOs,
61  const bool binary,
62  const List<floatScalar>& data,
63  const word& title,
64  const label nParcels
65 ) const
66 {
67  vtkOs
68  << title << ' ' << pTraits<Type>::nComponents << ' '
69  << nParcels << " float" << std::endl;
70  writeData(vtkOs, binary, data);
71 }
72 
73 
74 template<class CloudType>
76 {
77  label nParcels = this->owner().size();
78  DynamicList<floatScalar> position(3*nParcels);
79  DynamicList<floatScalar> U(3*nParcels);
80  DynamicList<floatScalar> d(nParcels);
81  DynamicList<floatScalar> age(nParcels);
82  DynamicList<floatScalar> rho(nParcels);
83 
84  forAllConstIter(typename CloudType, this->owner(), iter)
85  {
86  vtkTools::insert(iter().position(), position);
87  vtkTools::insert(iter().U(), U);
88  vtkTools::insert(iter().d(), d);
89  vtkTools::insert(iter().age(), age);
90  vtkTools::insert(iter().rho(), rho);
91  }
92 
93  reduce(nParcels, sumOp<label>());
94 
95 
96 binary_ = false;
97  if (Pstream::master())
98  {
99  // Create directory if does not exist
100  mkDir(this->outputTimeDir());
101 
102  // Open new file at start up
103 
104  const fileName fName = this->outputTimeDir()/(type() + ".vtk");
105  this->setModelProperty("file", fName);
106 
107  OFstream os(fName, binary_ ? IOstream::BINARY : IOstream::ASCII);
108  std::ostream& vtkOs = os.stdStream();
109 
110 
111  vtkTools::writeHeader(vtkOs, binary_, this->modelName().c_str());
112  vtkOs
113  << "DATASET POLYDATA" << std::endl
114  << "POINTS " << nParcels << " float" << std::endl;
115 
116  writeData(vtkOs, binary_, position);
117 
118  vtkOs
119  << "POINT_DATA " << nParcels << std::endl
120  << "FIELD attributes " << 4
121  << std::endl;
122 
123  writeFieldData<vector>(vtkOs, binary_, U, "U", nParcels);
124  writeFieldData<scalar>(vtkOs, binary_, d, "d", nParcels);
125  writeFieldData<scalar>(vtkOs, binary_, age, "age", nParcels);
126  writeFieldData<scalar>(vtkOs, binary_, rho, "rho", nParcels);
127  }
128 }
129 
130 
131 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
132 
133 template<class CloudType>
135 (
136  const dictionary& dict,
137  CloudType& owner,
138  const word& modelName
139 )
140 :
141  CloudFunctionObject<CloudType>(dict, owner, modelName, typeName),
142  binary_(dict.lookupOrDefault<bool>("binary", true))
143 {}
144 
145 
146 template<class CloudType>
148 (
149  const CloudToVTK<CloudType>& c
150 )
151 :
153  binary_(c.binary_)
154 {}
155 
156 
157 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
158 
159 template<class CloudType>
161 {}
162 
163 
164 // ************************************************************************* //
CloudToVTK.H
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::CloudToVTK::~CloudToVTK
virtual ~CloudToVTK()
Destructor.
Definition: CloudToVTK.C:160
Foam::accessOp
Definition: ListListOps.H:98
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::CloudToVTK::CloudToVTK
CloudToVTK(const dictionary &dict, CloudType &owner, const word &modelName)
Construct from dictionary.
Definition: CloudToVTK.C:135
Foam::CloudToVTK::writeData
void writeData(std::ostream &vtkOs, const bool binary, const List< floatScalar > &data) const
Helper function to write VTK data.
Definition: CloudToVTK.C:34
Foam::CloudToVTK::writeFieldData
void writeFieldData(std::ostream &vtkOs, const bool binary, const List< floatScalar > &data, const word &title, const label nParcels) const
Helper function to write VTK field data.
Definition: CloudToVTK.C:59
U
U
Definition: pEqn.H:46
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:43
Foam::label
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
Foam::Cloud::size
label size() const
Definition: Cloud.H:175
vtkTools.H
Foam::DSMCCloud
Templated base class for dsmc cloud.
Definition: DSMCCloud.H:68
dict
dictionary dict
Definition: searchingEngine.H:14
writeData
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::OFstream
Output to file stream.
Definition: OFstream.H:81
rho
rho
Definition: pEqn.H:3
Foam::OFstream::stdStream
virtual ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:136
Foam::sumOp
Definition: ops.H:162
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::CloudFunctionObject
Templated cloud function object base class.
Definition: CloudFunctionObject.H:56
Foam::CloudToVTK
Write cloud data in VTK format.
Definition: CloudToVTK.H:51
floatScalar.H
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::type
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:588
Foam::mkDir
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:419
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
write
Tcoeff write()
insert
timeIndices insert(timeIndex, timeDirs[timeI].value())
Foam::CloudToVTK::write
virtual void write()
Write post-processing info.
Definition: CloudToVTK.C:75