All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ensightPartIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 Description
25  Output for ensightPart
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "ensightPart.H"
30 #include "dictionary.H"
31 #include "IOstreams.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
36 (
37  ensightFile& os,
38  bool withDescription
39 ) const
40 {
41  os.write("part");
42  os.newline();
43 
44  os.write(number() + 1); // Ensight starts with 1
45  os.newline();
46 
47  if (withDescription)
48  {
49  os.write(name());
50  os.newline();
51  }
52 }
53 
54 
56 (
57  ensightFile& os,
58  const List<scalar>& field,
59  const labelUList& idList
60 ) const
61 {
62  if (notNull(idList))
63  {
64  forAll(idList, i)
65  {
66  if (idList[i] >= field.size() || std::isnan(field[idList[i]]))
67  {
68  os.writeUndef();
69  }
70  else
71  {
72  os.write(field[idList[i]]);
73  }
74 
75  os.newline();
76  }
77  }
78  else
79  {
80  // no idList => perNode
81  forAll(field, i)
82  {
83  if (std::isnan(field[i]))
84  {
85  os.writeUndef();
86  }
87  else
88  {
89  os.write(field[i]);
90  }
91 
92  os.newline();
93  }
94  }
95 }
96 
97 
98 
99 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100 
102 {
103  dictionary dict(is);
104  dict.lookup("id") >> number_;
105  dict.lookup("name") >> name_;
106 
107  offset_ = 0;
108  dict.readIfPresent("offset", offset_);
109 
110  // populate elemLists_
112 
113  forAll(elementTypes(), elemI)
114  {
115  word key(elementTypes()[elemI]);
116 
117  elemLists_[elemI].clear();
118  dict.readIfPresent(key, elemLists_[elemI]);
119 
120  size_ += elemLists_[elemI].size();
121  }
122 
123  is.check("ensightPart::reconstruct(Istream&)");
124 }
125 
126 
128 {
129  os << indent << type() << nl
131 
132  // Ensight starts with 1
133  writeEntry(os, "id", (number() + 1));
134  writeEntry(os, "name", name());
135  writeEntry(os, "offset", offset());
136  writeEntry(os, "size", size());
137 
138  os << decrIndent << indent << token::END_BLOCK << nl << endl;
139 
140  return true;
141 }
142 
143 
145 {
146  os << indent << type() << nl
148 
149  writeEntry(os, "id", number());
150  writeEntry(os, "name", name());
151  writeEntry(os, "offset", offset());
152 
153  forAll(elementTypes(), typeI)
154  {
155  word key(elementTypes()[typeI]);
156  if (elemLists_[typeI].size())
157  {
158  writeEntry(os, key, elemLists_[typeI]);
159  }
160  }
161 
162  os << decrIndent << indent << token::END_BLOCK << nl << endl;
163 
164  return true;
165 }
166 
167 
169 (
170  ensightGeoFile& os,
171  const pointField& points
172 ) const
173 {
174  if (size())
175  {
176  const localPoints ptList = calcLocalPoints();
177  const labelUList& pointMap = ptList.list;
178 
179  writeHeader(os, true);
180 
181  // write points
182  os.writeKeyword("coordinates");
183  os.write(ptList.nPoints);
184  os.newline();
185 
186  for (direction cmpt=0; cmpt < point::nComponents; ++cmpt)
187  {
188  forAll(pointMap, ptI)
189  {
190  if (pointMap[ptI] > -1)
191  {
192  os.write(points[ptI].component(cmpt));
193  os.newline();
194  }
195  }
196  }
197 
198  // write parts
199  forAll(elementTypes(), elemI)
200  {
201  if (elemLists_[elemI].size())
202  {
204  (
205  os,
206  elementTypes()[elemI],
207  elemLists_[elemI],
208  pointMap
209  );
210  }
211  }
212  }
213 }
214 
215 
217 (
218  ensightFile& os,
219  const List<scalar>& field,
220  const bool perNode
221 ) const
222 {
223  if (size() && field.size() && (os.allowUndef() || isFieldDefined(field)))
224  {
225  writeHeader(os);
226 
227  if (perNode)
228  {
229  os.writeKeyword("coordinates");
230  writeFieldList(os, field, labelUList::null());
231  }
232  else
233  {
234  forAll(elementTypes(), elemI)
235  {
236  const labelUList& idList = elemLists_[elemI];
237 
238  if (idList.size())
239  {
240  os.writeKeyword(elementTypes()[elemI]);
241  writeFieldList(os, field, idList);
242  }
243  }
244  }
245  }
246 }
247 
248 
250 (
251  ensightFile& os,
252  const List<scalar>& field0,
253  const List<scalar>& field1,
254  const List<scalar>& field2,
255  const bool perNode
256 ) const
257 {
258  if (size() && field0.size() && (os.allowUndef() || isFieldDefined(field0)))
259  {
260  writeHeader(os);
261 
262  if (perNode)
263  {
264  os.writeKeyword("coordinates");
265  writeFieldList(os, field0, labelUList::null());
266  writeFieldList(os, field1, labelUList::null());
267  writeFieldList(os, field2, labelUList::null());
268  }
269  else
270  {
271  forAll(elementTypes(), elemI)
272  {
273  const labelUList& idList = elemLists_[elemI];
274 
275  if (idList.size())
276  {
277  os.writeKeyword(elementTypes()[elemI]);
278  writeFieldList(os, field0, idList);
279  writeFieldList(os, field1, idList);
280  writeFieldList(os, field2, idList);
281  }
282  }
283  }
284  }
285 }
286 
287 
288 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
289 
290 Foam::Ostream& Foam::operator<<
291 (
292  Ostream& os,
293  const ensightPart& part
294 )
295 {
296  part.writeData(os);
297  return os;
298 }
299 
300 
301 Foam::ensightGeoFile& Foam::operator<<
302 (
303  ensightGeoFile& os,
304  const ensightPart& part
305 )
306 {
307  part.writeGeometry(os);
308  return os;
309 }
310 
311 
312 // ************************************************************************* //
Ostream & writeUndef()
Write undef value.
Definition: ensightFile.C:268
Ensight output with specialized write() for strings, integers and floats. Correctly handles binary wr...
Definition: ensightFile.H:47
label number_
Part number.
Definition: ensightPart.H:78
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool writeData(Ostream &) const
Write reconstruction information for the object.
label size() const
Number of elements in this part.
Definition: ensightPart.H:235
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:221
label nPoints
Number of points used.
Definition: ensightPart.H:109
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
uint8_t direction
Definition: direction.H:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
label number() const
Part number.
Definition: ensightPart.H:253
virtual localPoints calcLocalPoints() const
Track points used.
Definition: ensightPart.H:153
Specialized Ensight output with extra geometry file header.
Track the points used by the part and map global to local indices.
Definition: ensightPart.H:105
void writeScalarField(ensightFile &, const List< scalar > &field, const bool perNode=false) const
Write scalar field.
virtual Ostream & writeKeyword(const string &key)
Write element keyword with trailing newline, optionally with undef.
Definition: ensightFile.C:275
labelListList elemLists_
Simple labelList with a name.
Definition: ensightPart.H:84
labelList list
Map global to local indices.
Definition: ensightPart.H:112
void writeFieldList(ensightFile &os, const List< scalar > &field, const labelUList &idList) const
Write a scalar field for idList.
Definition: ensightPartIO.C:56
void writeHeader(ensightFile &, bool withDescription=false) const
Write the part header.
Definition: ensightPartIO.C:36
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void writeVectorField(ensightFile &, const List< scalar > &field0, const List< scalar > &field1, const List< scalar > &field2, const bool perNode=false) const
Write vector field components.
static const direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:99
label size_
Number of elements in this part.
Definition: ensightPart.H:90
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
const string & name() const
Part name or description.
Definition: ensightPart.H:259
A class for handling words, derived from string.
Definition: word.H:59
void reconstruct(Istream &)
Reconstruct part characteristics (eg, element types) from Istream.
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
virtual void writeGeometry(ensightGeoFile &) const
Write geometry.
Definition: ensightPart.H:307
virtual Ostream & writeKeyword(const string &key)
Write keyword with trailing newline.
virtual Ostream & write(const char *buf, std::streamsize count)
Binary write.
Definition: ensightFile.C:136
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
static bool allowUndef()
Return setting for whether &#39;undef&#39; values are allowed in results.
Definition: ensightFile.C:110
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
Base class for ensightPartCells and ensightPartFaces.
Definition: ensightPart.H:65
static const UList< T > & null()
Return a null UList.
Definition: UListI.H:51
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:235
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual void writeConnectivity(ensightGeoFile &, const word &key, const labelUList &idList, const labelUList &pointMap) const
Write connectivities.
Definition: ensightPart.H:160
bool isFieldDefined(const List< scalar > &) const
Check for fully defined fields.
Definition: ensightPart.C:44
bool notNull(const T &t)
Return true if t is not a reference to the nullObject of type T.
Definition: nullObjectI.H:52
void setSize(const label)
Reset size of List.
Definition: List.C:281
void newline()
Add carriage return to ascii stream.
Definition: ensightFile.C:259
label offset_
Start offset for elemLists_.
Definition: ensightPart.H:87
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
bool writeSummary(Ostream &) const
Write summary information about the object.
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:228
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
string name_
Part name (or description)
Definition: ensightPart.H:81
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:812
label offset() const
Offset for element ids.
Definition: ensightPart.H:289
virtual const List< word > & elementTypes() const
Definition: ensightPart.H:226