dxSurfaceWriter.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 \*---------------------------------------------------------------------------*/
25 
26 #include "dxSurfaceWriter.H"
27 
28 #include "OFstream.H"
29 #include "OSspecific.H"
30 
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  makeSurfaceWriterType(dxSurfaceWriter);
38 }
39 
40 
41 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
42 
43 void Foam::dxSurfaceWriter::writeGeometry
44 (
45  Ostream& os,
46  const pointField& points,
47  const faceList& faces
48 )
49 {
50  // Write vertex coordinates
51 
52  os << "# The irregular positions" << nl
53  << "object 1 class array type float rank 1 shape 3 items "
54  << points.size() << " data follows" << nl;
55 
56  forAll(points, pointi)
57  {
58  const point& pt = points[pointi];
59 
60  os << float(pt.x()) << ' ' << float(pt.y()) << ' ' << float(pt.z())
61  << nl;
62  }
63  os << nl;
64 
65  // Write triangles
66  os << "# The irregular connections (triangles)" << nl
67  << "object 2 class array type int rank 1 shape 3 items "
68  << faces.size() << " data follows" << nl;
69 
70  forAll(faces, facei)
71  {
72  const face& f = faces[facei];
73 
74  if (f.size() != 3)
75  {
77  << "Face " << facei << " vertices " << f
78  << " is not a triangle."
79  << exit(FatalError);
80  }
81 
82  os << f[0] << ' ' << f[1] << ' ' << f[2] << nl;
83  }
84  os << "attribute \"element type\" string \"triangles\"" << nl
85  << "attribute \"ref\" string \"positions\"" << nl << nl;
86 }
87 
88 
89 void Foam::dxSurfaceWriter::writeTrailer(Ostream& os, const bool isNodeValues)
90 {
91  if (isNodeValues)
92  {
93  os << nl << "attribute \"dep\" string \"positions\""
94  << nl << nl;
95  }
96  else
97  {
98  os << nl << "attribute \"dep\" string \"connections\""
99  << nl << nl;
100  }
101 
102  os << "# the field, with three components: \"positions\","
103  << " \"connections\", and \"data\"" << nl
104  << "object \"irregular positions irregular "
105  << "connections\" class field"
106  << nl
107  << "component \"positions\" value 1" << nl
108  << "component \"connections\" value 2" << nl
109  << "component \"data\" value 3" << nl;
110 
111  os << "end" << endl;
112 }
113 
114 
115 namespace Foam
116 {
117  template<>
118  void Foam::dxSurfaceWriter::writeData
119  (
120  Ostream& os,
121  const Field<scalar>& values
122  )
123  {
124  os << "object 3 class array type float rank 0 items "
125  << values.size() << " data follows" << nl;
126 
127  forAll(values, elemI)
128  {
129  os << float(values[elemI]) << nl;
130  }
131  }
132 
133 
134  template<>
135  void Foam::dxSurfaceWriter::writeData
136  (
137  Ostream& os,
138  const Field<vector>& values
139  )
140  {
141  os << "object 3 class array type float rank 1 shape 3 items "
142  << values.size() << " data follows" << nl;
143 
144  forAll(values, elemI)
145  {
146  os << float(values[elemI].x()) << ' '
147  << float(values[elemI].y()) << ' '
148  << float(values[elemI].z()) << nl;
149  }
150  }
151 
152 
153  template<>
154  void Foam::dxSurfaceWriter::writeData
155  (
156  Ostream& os,
157  const Field<sphericalTensor>& values
158  )
159  {
160  os << "object 3 class array type float rank 0 items "
161  << values.size() << " data follows" << nl;
162 
163  forAll(values, elemI)
164  {
165  os << float(values[elemI][0]) << nl;
166  }
167  }
168 
169 
170  template<>
171  void Foam::dxSurfaceWriter::writeData
172  (
173  Ostream& os,
174  const Field<symmTensor>& values
175  )
176  {
177  os << "object 3 class array type float rank 2 shape 3 items "
178  << values.size() << " data follows" << nl;
179 
180  forAll(values, elemI)
181  {
182  const symmTensor& t = values[elemI];
183 
184  os << float(t.xx()) << ' ' << float(t.xy()) << ' ' << float(t.xz())
185  << float(t.xy()) << ' ' << float(t.yy()) << ' ' << float(t.yz())
186  << float(t.xz()) << ' ' << float(t.yz()) << ' ' << float(t.zz())
187  << nl;
188  }
189  }
190 
191 
192  // Write Field<tensor> in DX format
193  template<>
194  inline void Foam::dxSurfaceWriter::writeData
195  (
196  Ostream& os,
197  const Field<tensor>& values
198  )
199  {
200  os << "object 3 class array type float rank 2 shape 3 items "
201  << values.size() << " data follows" << nl;
202 
203  forAll(values, elemI)
204  {
205  const tensor& t = values[elemI];
206 
207  os << float(t.xx()) << ' ' << float(t.xy()) << ' ' << float(t.xz())
208  << float(t.yx()) << ' ' << float(t.yy()) << ' ' << float(t.yz())
209  << float(t.zx()) << ' ' << float(t.zy()) << ' ' << float(t.zz())
210  << nl;
211  }
212  }
213 }
214 
215 
216 // arbitrary field
217 template<class Type>
218 inline void Foam::dxSurfaceWriter::writeData
219 (
220  Ostream& os,
221  const Field<Type>& values
222 )
223 {
224  os << "object 3 class array type float rank 0 items "
225  << values.size() << " data follows" << nl;
226 
227  forAll(values, elemI)
228  {
229  os << float(0.0) << nl;
230  }
231 }
232 
233 
234 template<class Type>
235 void Foam::dxSurfaceWriter::writeTemplate
236 (
237  const fileName& outputDir,
238  const fileName& surfaceName,
239  const pointField& points,
240  const faceList& faces,
241  const word& fieldName,
242  const Field<Type>& values,
243  const bool isNodeValues,
244  const bool verbose
245 ) const
246 {
247  if (!isDir(outputDir))
248  {
249  mkDir(outputDir);
250  }
251 
252  OFstream os
253  (
254  outputDir/fieldName + '_' + surfaceName + ".dx"
255  );
256 
257  if (verbose)
258  {
259  Info<< "Writing field " << fieldName << " to " << os.name() << endl;
260  }
261 
262  writeGeometry(os, points, faces);
263  writeData(os, values);
264  writeTrailer(os, isNodeValues);
265 }
266 
267 
268 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
269 
271 :
272  surfaceWriter()
273 {}
274 
275 
276 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
277 
279 {}
280 
281 
282 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
283 
284 // create write methods
286 
287 
288 // ************************************************************************* //
const Cmpt & xx() const
Definition: SymmTensorI.H:87
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
const Cmpt & yy() const
Definition: TensorI.H:181
A class for handling file names.
Definition: fileName.H:69
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 Cmpt & xz() const
Definition: SymmTensorI.H:99
Output to file stream.
Definition: OFstream.H:81
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
List< face > faceList
Definition: faceListFwd.H:43
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
const Cmpt & zy() const
Definition: TensorI.H:202
const Cmpt & yz() const
Definition: TensorI.H:188
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:486
A surfaceWriter for OpenDX format.
const Cmpt & xz() const
Definition: TensorI.H:167
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
scalar y
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
const Cmpt & xy() const
Definition: SymmTensorI.H:93
A class for handling words, derived from string.
Definition: word.H:59
Convenience macros for instantiating writer methods for surfaceWriter classes.
const Cmpt & yz() const
Definition: SymmTensorI.H:111
const Cmpt & xx() const
Definition: TensorI.H:153
defineSurfaceWriterWriteFields(nastranSurfaceWriter)
const Cmpt & xy() const
Definition: TensorI.H:160
dxSurfaceWriter()
Construct null.
const Cmpt & yx() const
Definition: TensorI.H:174
virtual ~dxSurfaceWriter()
Destructor.
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
const Cmpt & zz() const
Definition: TensorI.H:209
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
labelList f(nPoints)
const Cmpt & yy() const
Definition: SymmTensorI.H:105
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:295
vector point
Point is a vector.
Definition: point.H:41
const Cmpt & zz() const
Definition: SymmTensorI.H:117
messageStream Info
Base class for surface writers.
Definition: surfaceWriter.H:54
const Cmpt & zx() const
Definition: TensorI.H:195
const fileName & name() const
Return the name of the stream.
Definition: OFstream.H:118
Namespace for OpenFOAM.
makeSurfaceWriterType(dxSurfaceWriter)