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 | Copyright (C) 2011-2012 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 "ensightSetWriter.H"
27 #include "coordSet.H"
28 #include "OFstream.H"
30 #include "IOmanip.H"
31 #include "foamVersion.H"
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
58  this->getBaseName(points, valueSetNames)
59  //+ '_'
60  //+ pTraits<Type>::typeName
61  + ".case";
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  const fileName base(os.name().lessExt());
75  const fileName meshFile(base + ".mesh");
76 
77  // Write .case file
78  os << "FORMAT" << nl
79  << "type: ensight gold" << nl
80  << nl
81  << "GEOMETRY" << nl
82  << "model: 1 " << meshFile.name().c_str() << nl
83  << nl
84  << "VARIABLE"
85  << nl;
86  forAll(valueSetNames, setI)
87  {
88  fileName dataFile(base + ".***." + valueSetNames[setI]);
89 
90  os.setf(ios_base::left);
91  os << pTraits<Type>::typeName
92  << " per node: 1 "
93  << setw(15) << valueSetNames[setI]
94  << " " << dataFile.name().c_str()
95  << nl;
96  }
97  os << nl
98  << "TIME" << nl
99  << "time set: 1" << nl
100  << "number of steps: 1" << nl
101  << "filename start number: 0" << nl
102  << "filename increment: 1" << nl
103  << "time values:" << nl
104  << "0.00000e+00" << nl;
105 
106  // Write .mesh file
107  {
108  string desc = string("written by OpenFOAM-") + Foam::FOAMversion;
109  OFstream os(meshFile);
110  os.setf(ios_base::scientific, ios_base::floatfield);
111  os.precision(5);
112 
113  os << "EnSight Geometry File" << nl
114  << desc.c_str() << nl
115  << "node id assign" << nl
116  << "element id assign" << nl
117  << "part" << nl
118  << setw(10) << 1 << nl
119  << "internalMesh" << nl
120  << "coordinates" << nl
121  << setw(10) << points.size() << nl;
122 
123  for (direction cmpt = 0; cmpt < vector::nComponents; cmpt++)
124  {
125  forAll(points, pointI)
126  {
127  const scalar comp = points[pointI][cmpt];
128  if (mag(comp) >= scalar(floatScalarVSMALL))
129  {
130  os << setw(12) << comp << nl;
131  }
132  else
133  {
134  os << setw(12) << scalar(0) << nl;
135  }
136  }
137  }
138  os << "point" << nl
139  << setw(10) << points.size() << nl;
140  forAll(points, pointI)
141  {
142  os << setw(10) << pointI+1 << nl;
143  }
144  }
145 
146  // Write data files
147  forAll(valueSetNames, setI)
148  {
149  fileName dataFile(base + ".000." + valueSetNames[setI]);
150  OFstream os(dataFile);
151  os.setf(ios_base::scientific, ios_base::floatfield);
152  os.precision(5);
153  {
154  os << pTraits<Type>::typeName << nl
155  << "part" << nl
156  << setw(10) << 1 << nl
157  << "coordinates" << nl;
158  for (direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
159  {
160  const scalarField fld(valueSets[setI]->component(cmpt));
161  forAll(fld, i)
162  {
163  if (mag(fld[i]) >= scalar(floatScalarVSMALL))
164  {
165  os << setw(12) << fld[i] << nl;
166  }
167  else
168  {
169  os << setw(12) << scalar(0) << nl;
170  }
171  }
172  }
173  }
174  }
175 }
176 
177 
178 template<class Type>
180 (
181  const bool writeTracks,
182  const PtrList<coordSet>& tracks,
183  const wordList& valueSetNames,
184  const List<List<Field<Type> > >& valueSets,
185  Ostream& os
186 ) const
187 {
188  const fileName base(os.name().lessExt());
189  const fileName meshFile(base + ".mesh");
190 
191  // Write .case file
192  os << "FORMAT" << nl
193  << "type: ensight gold" << nl
194  << nl
195  << "GEOMETRY" << nl
196  << "model: 1 " << meshFile.name().c_str() << nl
197  << nl
198  << "VARIABLE"
199  << nl;
200  forAll(valueSetNames, setI)
201  {
202  fileName dataFile(base + ".***." + valueSetNames[setI]);
203 
204  os.setf(ios_base::left);
205  os << pTraits<Type>::typeName
206  << " per node: 1 "
207  << setw(15) << valueSetNames[setI]
208  << " " << dataFile.name().c_str()
209  << nl;
210  }
211  os << nl
212  << "TIME" << nl
213  << "time set: 1" << nl
214  << "number of steps: 1" << nl
215  << "filename start number: 0" << nl
216  << "filename increment: 1" << nl
217  << "time values:" << nl
218  << "0.00000e+00" << nl;
219 
220  // Write .mesh file
221  {
222  string desc = string("written by OpenFOAM-") + Foam::FOAMversion;
223  OFstream os(meshFile);
224  os.setf(ios_base::scientific, ios_base::floatfield);
225  os.precision(5);
226  os << "EnSight Geometry File" << nl
227  << desc.c_str() << nl
228  << "node id assign" << nl
229  << "element id assign" << nl;
230 
231  forAll(tracks, trackI)
232  {
233  const coordSet& points = tracks[trackI];
234 
235  os << "part" << nl
236  << setw(10) << trackI+1 << nl
237  << "internalMesh" << nl
238  << "coordinates" << nl
239  << setw(10) << points.size() << nl;
240 
241  for (direction cmpt = 0; cmpt < vector::nComponents; cmpt++)
242  {
243  forAll(points, pointI)
244  {
245  const scalar comp = points[pointI][cmpt];
246  if (mag(comp) >= scalar(floatScalarVSMALL))
247  {
248  os << setw(12) << comp << nl;
249  }
250  else
251  {
252  os << setw(12) << scalar(0) << nl;
253  }
254  }
255  }
256 
257  if (writeTracks)
258  {
259  os << "bar2" << nl
260  << setw(10) << points.size()-1 << nl;
261  for (label i = 0; i < points.size()-1; i++)
262  {
263  os << setw(10) << i+1
264  << setw(10) << i+2
265  << nl;
266  }
267  }
268  }
269  }
270 
271 
272  // Write data files
273  forAll(valueSetNames, setI)
274  {
275  fileName dataFile(base + ".000." + valueSetNames[setI]);
276  OFstream os(dataFile);
277  os.setf(ios_base::scientific, ios_base::floatfield);
278  os.precision(5);
279  {
280  os << pTraits<Type>::typeName << nl;
281 
282  const List<Field<Type> >& fieldVals = valueSets[setI];
283  forAll(fieldVals, trackI)
284  {
285  os << "part" << nl
286  << setw(10) << trackI+1 << nl
287  << "coordinates" << nl;
288 
289  for
290  (
291  direction cmpt = 0;
292  cmpt < pTraits<Type>::nComponents;
293  cmpt++
294  )
295  {
296  const scalarField fld(fieldVals[trackI].component(cmpt));
297  forAll(fld, i)
298  {
299  if (mag(fld[i]) >= scalar(floatScalarVSMALL))
300  {
301  os << setw(12) << fld[i] << nl;
302  }
303  else
304  {
305  os << setw(12) << scalar(0) << nl;
306  }
307  }
308  }
309  }
310  }
311  }
312 }
313 
314 
315 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
unsigned char direction
Definition: direction.H:43
word name() const
Return file name (part beyond last /)
Definition: fileName.C:206
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:61
dimensioned< scalar > mag(const dimensioned< Type > &)
Holds list of sampling positions.
Definition: coordSet.H:49
virtual void write(const coordSet &, const wordList &, const List< const Field< Type > * > &, Ostream &) const
General entry point for writing.
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 character strings derived from std::string.
Definition: string.H:74
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
ensightSetWriter()
Construct null.
const char *const FOAMversion
fileName getBaseName(const coordSet &, const wordList &) const
Generates filename from coordSet and sampled fields.
Definition: writer.C:62
Number of components in this vector space.
Definition: VectorSpace.H:88
static const char nl
Definition: Ostream.H:260
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:290
static const floatScalar floatScalarVSMALL
Definition: floatScalar.H:59
#define forAll(list, i)
Definition: UList.H:421
virtual ~ensightSetWriter()
Destructor.
Macros for easy insertion into run-time selection tables.
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Base class for graphics format writing. Entry points are.
Definition: writer.H:78
Pre-declare SubField and related Field type.
Definition: Field.H:57
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 ))
Istream and Ostream manipulators taking arguments.
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
virtual fileName getFileName(const coordSet &, const wordList &) const
Generate file name with correct extension.
A class for handling file names.
Definition: fileName.H:69
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:496
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileName.C:307
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
IOstream & scientific(IOstream &io)
Definition: IOstream.H:582