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