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-2016 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 patches(calcPatches(faceMap));
48 
49  // Print patch names as comment
50  os << "# Patches:" << endl;
52  {
53  os << "# " << patchi << " "
54  << patches[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 
79  {
80  // Print all faces belonging to this patch
81 
82  for
83  (
84  label patchFacei = 0;
85  patchFacei < patches[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 patches(calcPatches(faceMap));
136 
138  {
139  forAll(patches[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  << "Illegal field size " << field.size() << " is not equal "
196  << " to number of faces " << size() << " or to number "
197  << " of points " << nPoints() << exit(FatalError);
198  }
199 
200  writeDXTrailer(os);
201 
202  os << "end" << endl;
203 }
204 
205 
206 // Geometry + vector data
207 void triSurface::writeDX(const vectorField& field, Ostream& os) const
208 {
209  writeDXGeometry(false, os);
210 
211  if (field.size() == size())
212  {
213  // Connections dependent data
214  os << "object 3 class array type float rank 1 shape 3 items "
215  << field.size() << " data follows" << endl;
216  forAll(field, facei)
217  {
218  os << field[facei].x() << ' '
219  << field[facei].y() << ' '
220  << field[facei].z() << endl;
221  }
222  os << endl
223  << "attribute \"dep\" string \"connections\"" << endl << endl;
224  }
225  else if (field.size() == nPoints())
226  {
227  // Positions dependent data
228  os << "object 3 class array type float rank 1 shape 3 items "
229  << field.size() << " data follows" << endl;
230  forAll(field, pointi)
231  {
232  os << field[pointi].x() << ' '
233  << field[pointi].y() << ' '
234  << field[pointi].z() << endl;
235  }
236  os << endl
237  << "attribute \"dep\" string \"positions\"" << endl << endl;
238  }
239  else
240  {
242  << "Illegal field size " << field.size() << " is not equal "
243  << " to number of faces " << size() << " or to number "
244  << " of points " << nPoints() << exit(FatalError);
245  }
246 
247  writeDXTrailer(os);
248 
249  os << "end" << endl;
250 }
251 
252 
253 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
254 
255 } // End namespace Foam
256 
257 // ************************************************************************* //
label nPoints() const
Return number of points supporting patch faces.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
const List< labelledTri > & localFaces() const
Return patch faces addressing into local point list.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
List< surfacePatch > surfacePatchList
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
const geometricSurfacePatchList & patches() const
Definition: triSurface.H:314
List< label > labelList
A List of labels.
Definition: labelList.H:56
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:262
labelList f(nPoints)
label size() const
Return the number of elements in the UList.
label patchi
vector point
Point is a vector.
Definition: point.H:41
const Field< point > & localPoints() const
Return pointField of points in patch.
Namespace for OpenFOAM.