ensightSetWriter.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) 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 "ensightSetWriter.H"
30 #include "coordSet.H"
31 #include "IOmanip.H"
32 #include "ensightGeoFile.H"
33 #include "ensightPTraits.H"
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
62  this->getBaseName(points, valueSetNames)
63  //+ '_'
64  //+ ensightPTraits<Type>::typeName
65  + ".case";
66 }
67 
68 
69 template<class Type>
71 (
72  const coordSet& points,
73  const wordList& valueSetNames,
74  const List<const Field<Type>*>& valueSets,
75  Ostream& os
76 ) const
77 {
78  const fileName base(os.name().lessExt());
79  const fileName meshFile(base + ".mesh");
80 
81  // Write .case file
82  os << "FORMAT" << nl
83  << "type: ensight gold" << nl
84  << nl
85  << "GEOMETRY" << nl
86  << "model: 1 " << meshFile.name().c_str() << nl
87  << nl
88  << "VARIABLE"
89  << nl;
90 
91  for (const word& valueName : valueSetNames)
92  {
93  fileName dataFile(base + ".***." + valueName);
94 
95  os.setf(ios_base::left);
96  os << ensightPTraits<Type>::typeName
97  << " per node: 1 "
98  << setw(15) << valueName
99  << " " << dataFile.name().c_str()
100  << nl;
101  }
102  os << nl
103  << "TIME" << nl
104  << "time set: 1" << nl
105  << "number of steps: 1" << nl
106  << "filename start number: 0" << nl
107  << "filename increment: 1" << nl
108  << "time values:" << nl
109  << "0.00000e+00" << nl;
110 
111  // Write .mesh file
112  {
113  string desc("Written by OpenFOAM");
114  OFstream os(meshFile);
115  os.setf(ios_base::scientific, ios_base::floatfield);
116  os.precision(5);
117 
118  os << "Ensight Geometry File" << nl
119  << desc.c_str() << nl
120  << "node id assign" << nl
121  << "element id assign" << nl
122  << "part" << nl
123  << setw(10) << 1 << nl
124  << "internalMesh" << nl
125  << "coordinates" << nl
126  << setw(10) << points.size() << nl;
127 
128  for (direction cmpt = 0; cmpt < vector::nComponents; ++cmpt)
129  {
130  for (const point& p : points)
131  {
132  const float comp = narrowFloat(p[cmpt]);
133  os << setw(12) << comp << nl;
134  }
135  }
136  os << "point" << nl
137  << setw(10) << points.size() << nl;
138  forAll(points, pointi)
139  {
140  os << setw(10) << pointi+1 << nl;
141  }
142  }
143 
144  // Write data files
145  forAll(valueSetNames, seti)
146  {
147  const word& valueName = valueSetNames[seti];
148  const Field<Type>& fld = *(valueSets[seti]);
149 
150  fileName dataFile(base + ".000." + valueName);
151  OFstream os(dataFile);
152  os.setf(ios_base::scientific, ios_base::floatfield);
153  os.precision(5);
154 
155  os << ensightPTraits<Type>::typeName << nl
156  << "part" << nl
157  << setw(10) << 1 << nl
158  << "coordinates" << nl;
159 
160  for (direction d=0; d < pTraits<Type>::nComponents; ++d)
161  {
163 
164  for (const Type& val : fld)
165  {
166  const float comp = narrowFloat(component(val, cmpt));
167  os << setw(12) << comp << nl;
168  }
169  }
170  }
171 }
172 
173 
174 template<class Type>
176 (
177  const bool writeTracks,
178  const List<scalarField>& times,
179  const PtrList<coordSet>& tracks,
180  const wordList& valueSetNames,
181  const List<List<Field<Type>>>& valueSets,
182  Ostream& os
183 ) const
184 {
185  const fileName base(os.name().lessExt());
186  const fileName meshFile(base + ".mesh");
187 
188  // Write .case file
189  os << "FORMAT" << nl
190  << "type: ensight gold" << nl
191  << nl
192  << "GEOMETRY" << nl
193  << "model: 1 " << meshFile.name().c_str() << nl
194  << nl
195  << "VARIABLE"
196  << nl;
197 
198  for (const word& valueName : valueSetNames)
199  {
200  fileName dataFile(base + ".***." + valueName);
201 
202  os.setf(ios_base::left);
203  os << ensightPTraits<Type>::typeName
204  << " per node: 1 "
205  << setw(15) << valueName
206  << " " << dataFile.name().c_str()
207  << nl;
208  }
209  os << nl
210  << "TIME" << nl
211  << "time set: 1" << nl
212  << "number of steps: 1" << nl
213  << "filename start number: 0" << nl
214  << "filename increment: 1" << nl
215  << "time values:" << nl
216  << "0.00000e+00" << nl;
217 
218  // Write .mesh file
219  {
220  string desc("Written by OpenFOAM");
221  OFstream os(meshFile);
222  os.setf(ios_base::scientific, ios_base::floatfield);
223  os.precision(5);
224  os << "Ensight Geometry File" << nl
225  << desc.c_str() << nl
226  << "node id assign" << nl
227  << "element id assign" << nl;
228 
229  forAll(tracks, tracki)
230  {
231  const coordSet& points = tracks[tracki];
232 
233  os << "part" << nl
234  << setw(10) << tracki+1 << nl
235  << "internalMesh" << nl
236  << "coordinates" << nl
237  << setw(10) << points.size() << nl;
238 
239  for (direction cmpt = 0; cmpt < vector::nComponents; ++cmpt)
240  {
241  for (const point& p : points)
242  {
243  const float comp = narrowFloat(p[cmpt]);
244  os << setw(12) << comp << nl;
245  }
246  }
247 
248  if (writeTracks)
249  {
250  os << "bar2" << nl
251  << setw(10) << points.size()-1 << nl;
252  for (label i = 0; i < points.size()-1; i++)
253  {
254  os << setw(10) << i+1
255  << setw(10) << i+2
256  << nl;
257  }
258  }
259  }
260  }
261 
262 
263  // Write data files
264  forAll(valueSetNames, seti)
265  {
266  const word& valueName = valueSetNames[seti];
267  const List<Field<Type>>& fieldVals = valueSets[seti];
268 
269  fileName dataFile(base + ".000." + valueName);
270  OFstream os(dataFile);
271  os.setf(ios_base::scientific, ios_base::floatfield);
272  os.precision(5);
273  {
274  os << ensightPTraits<Type>::typeName << nl;
275 
276  forAll(fieldVals, tracki)
277  {
278  const Field<Type>& fld = fieldVals[tracki];
279 
280  os << "part" << nl
281  << setw(10) << tracki+1 << nl
282  << "coordinates" << nl;
283 
284  for (direction d=0; d < pTraits<Type>::nComponents; ++d)
285  {
286  const direction cmpt =
288 
289  for (const Type& val : fld)
290  {
291  const float comp = narrowFloat(component(val, cmpt));
292  os << setw(12) << comp << nl;
293  }
294  }
295  }
296  }
297  }
298 }
299 
300 
301 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:44
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::OFstream::name
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: OSstream.H:107
Foam::ensightPTraits
Ensight names and component order for base types.
Definition: ensightPTraits.H:53
coordSet.H
Foam::fileName::name
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:199
Foam::ensightSetWriter::getFileName
virtual fileName getFileName(const coordSet &, const wordList &) const
Generate file name with correct extension.
Definition: ensightSetWriter.C:56
Foam::fileName::lessExt
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:230
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::ensightSetWriter::ensightSetWriter
ensightSetWriter()
Default construct.
Definition: ensightSetWriter.C:39
Foam::Field
Generic templated field type.
Definition: Field.H:63
ensightPTraits.H
IOmanip.H
Istream and Ostream manipulators taking arguments.
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
ensightSetWriter.H
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::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::narrowFloat
float narrowFloat(const double val)
Type narrowing from double to float.
Definition: scalar.H:199
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::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:464
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:378
Foam::OSstream::precision
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:326
ensightGeoFile.H
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Vector< scalar >
Foam::List< word >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::ensightSetWriter::write
virtual void write(const coordSet &, const wordList &, const List< const Field< Type > * > &, Ostream &) const
General entry point for writing.
Definition: ensightSetWriter.C:71
Foam::VectorSpace< Vector< scalar >, scalar, 3 >::nComponents
static constexpr direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:101