writeDX.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 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  OpenDX format. Both data-only and scalar/vector data.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "triSurface.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 
36 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
37 
38 // Geometry (positions + connections)
39 // writeSorted: sort acc. to patch
40 void triSurface::writeDXGeometry
41 (
42  const bool writeSorted,
43  Ostream& os
44 ) const
45 {
47  surfacePatchList myPatches(calcPatches(faceMap));
48 
49  // Print patch names as comment
50  os << "# Patches:" << endl;
51  forAll(myPatches, patchI)
52  {
53  os << "# " << patchI << " "
54  << myPatches[patchI].name() << endl;
55  }
56  os << nl << endl;
57 
58  // Write vertex coordinates
59 
60  os << "# The irregular positions" << endl
61  << "object 1 class array type float rank 1 shape 3 items "
62  << nPoints() << " data follows" << endl;
63  forAll(localPoints(), pointI)
64  {
65  const point& pt = localPoints()[pointI];
66  os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
67  }
68  os << endl;
69 
70  os << "# The irregular connections (triangles)" << endl
71  << "object 2 class array type int rank 1 shape 3 items "
72  << size() << " data follows" << endl;
73 
74  if (writeSorted)
75  {
76  label faceIndex = 0;
77 
78  forAll(myPatches, patchI)
79  {
80  // Print all faces belonging to this patch
81 
82  for
83  (
84  label patchFaceI = 0;
85  patchFaceI < myPatches[patchI].size();
86  patchFaceI++
87  )
88  {
89  const label faceI = faceMap[faceIndex++];
90  const labelledTri& f = localFaces()[faceI];
91 
92  os << f[0] << ' ' << f[1] << ' ' << f[2] << endl;
93  }
94  }
95  }
96  else
97  {
98  forAll(*this, faceI)
99  {
100  const labelledTri& f = localFaces()[faceI];
101 
102  os << f[0] << ' ' << f[1] << ' ' << f[2] << endl;
103  }
104  }
105  os << "attribute \"element type\" string \"triangles\"" << endl
106  << "attribute \"ref\" string \"positions\"" << endl << endl;
107 }
108 
109 
110 // Standard trailer
111 void triSurface::writeDXTrailer(Ostream& os) const
112 {
113  os << "# the field, with three components: \"positions\", \"connections\""
114  << ", and \"data\"" << endl
115  << "object \"irregular positions irregular connections\" class field"
116  << endl
117  << "component \"positions\" value 1" << endl
118  << "component \"connections\" value 2" << endl
119  << "component \"data\" value 3" << endl;
120 }
121 
122 
123 // Geometry only (data field is either faceIndex or patchIndex)
124 void triSurface::writeDX(const bool writeSorted, Ostream& os) const
125 {
126  writeDXGeometry(writeSorted, os);
127 
128  os << "object 3 class array type float rank 0 items " << size()
129  << " data follows" << endl;
130  if (writeSorted)
131  {
132  // Write patch number as data
133 
135  surfacePatchList myPatches(calcPatches(faceMap));
136 
137  forAll(myPatches, patchI)
138  {
139  forAll(myPatches[patchI], patchFaceI)
140  {
141  os << patchI << endl;
142  }
143  }
144  }
145  else
146  {
147  // Write face number as data
148 
149  forAll(*this, faceI)
150  {
151  os << faceI << endl;
152  }
153  }
154 
155  os << endl << "attribute \"dep\" string \"connections\"" << endl << endl;
156 
157  writeDXTrailer(os);
158 
159  os << "end" << endl;
160 }
161 
162 
163 // Geometry + scalar data
164 void triSurface::writeDX(const scalarField& field, Ostream& os) const
165 {
166  writeDXGeometry(false, os);
167 
168  if (field.size() == size())
169  {
170  // Connections dependent data
171  os << "object 3 class array type float rank 0 items " << field.size()
172  << " data follows" << endl;
173  forAll(field, faceI)
174  {
175  os << field[faceI] << endl;
176  }
177  os << endl
178  << "attribute \"dep\" string \"connections\"" << endl << endl;
179  }
180  else if (field.size() == nPoints())
181  {
182  // Positions dependent data
183  os << "object 3 class array type float rank 0 items " << field.size()
184  << " data follows" << endl;
185  forAll(field, pointI)
186  {
187  os << field[pointI] << endl;
188  }
189  os << endl
190  << "attribute \"dep\" string \"positions\"" << endl << endl;
191  }
192  else
193  {
195  (
196  "writeDX(const scalarField&, Ostream&)"
197  ) << "Illegal field size " << field.size() << " is not equal "
198  << " to number of faces " << size() << " or to number "
199  << " of points " << nPoints() << exit(FatalError);
200  }
201 
202  writeDXTrailer(os);
203 
204  os << "end" << endl;
205 }
206 
207 
208 // Geometry + vector data
209 void triSurface::writeDX(const vectorField& field, Ostream& os) const
210 {
211  writeDXGeometry(false, os);
212 
213  if (field.size() == size())
214  {
215  // Connections dependent data
216  os << "object 3 class array type float rank 1 shape 3 items "
217  << field.size() << " data follows" << endl;
218  forAll(field, faceI)
219  {
220  os << field[faceI].x() << ' '
221  << field[faceI].y() << ' '
222  << field[faceI].z() << endl;
223  }
224  os << endl
225  << "attribute \"dep\" string \"connections\"" << endl << endl;
226  }
227  else if (field.size() == nPoints())
228  {
229  // Positions dependent data
230  os << "object 3 class array type float rank 1 shape 3 items "
231  << field.size() << " data follows" << endl;
232  forAll(field, pointI)
233  {
234  os << field[pointI].x() << ' '
235  << field[pointI].y() << ' '
236  << field[pointI].z() << endl;
237  }
238  os << endl
239  << "attribute \"dep\" string \"positions\"" << endl << endl;
240  }
241  else
242  {
244  (
245  "writeDX(const vectorField&, Ostream&)"
246  ) << "Illegal field size " << field.size() << " is not equal "
247  << " to number of faces " << size() << " or to number "
248  << " of points " << nPoints() << exit(FatalError);
249  }
250 
251  writeDXTrailer(os);
252 
253  os << "end" << endl;
254 }
255 
256 
257 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 
259 } // End namespace Foam
260 
261 // ************************************************************************* //
vector point
Point is a vector.
Definition: point.H:41
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
labelList f(nPoints)
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
label nPoints() const
Return number of points supporting patch faces.
Namespace for OpenFOAM.
List< surfacePatch > surfacePatchList
const Field< point > & localPoints() const
Return pointField of points in patch.
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
#define forAll(list, i)
Definition: UList.H:421
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
List< label > labelList
A List of labels.
Definition: labelList.H:56
label size() const
Return the number of elements in the UList.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
const List< labelledTri > & localFaces() const
Return patch faces addressing into local point list.