boundaryDataSurfaceWriter.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015 OpenFOAM Foundation
9  Copyright (C) 2015-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
30 #include "argList.H"
31 #include "OFstream.H"
32 #include "OSspecific.H"
33 #include "IOmanip.H"
34 #include "Time.H"
35 #include "pointIOField.H"
36 #include "primitivePatch.H"
37 #include "surfaceWriterMethods.H"
39 
40 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
41 
42 namespace Foam
43 {
44 namespace surfaceWriters
45 {
46  defineTypeName(boundaryDataWriter);
47  addToRunTimeSelectionTable(surfaceWriter, boundaryDataWriter, word);
49 }
50 }
51 
52 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
56 :
57  surfaceWriter(),
58  streamOpt_(),
59  header_(true),
60  writeNormal_(false)
61 {}
62 
63 
65 (
66  const dictionary& options
67 )
68 :
69  surfaceWriter(options),
70  streamOpt_
71  (
72  IOstreamOption::formatEnum("format", options, IOstreamOption::ASCII),
73  IOstreamOption::compressionEnum("compression", options)
74  ),
75  header_(options.getOrDefault("header", true)),
76  writeNormal_(options.getOrDefault("normal", false))
77 {}
78 
79 
81 (
82  const meshedSurf& surf,
83  const fileName& outputPath,
84  bool parallel,
85  const dictionary& options
86 )
87 :
89 {
90  open(surf, outputPath, parallel);
91 }
92 
93 
95 (
96  const pointField& points,
97  const faceList& faces,
98  const fileName& outputPath,
99  bool parallel,
100  const dictionary& options
101 )
102 :
103  boundaryDataWriter(options)
104 {
105  open(points, faces, outputPath, parallel);
106 }
107 
108 
109 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
110 
111 void Foam::surfaceWriters::boundaryDataWriter::serialWriteGeometry
112 (
113  const regIOobject& iopts,
114  const meshedSurf& surf
115 )
116 {
117  const pointField& points = surf.points();
118  const faceList& faces = surf.faces();
119 
120  if (verbose_)
121  {
122  if (this->isPointData())
123  {
124  Info<< "Writing points: " << iopts.objectPath() << endl;
125  }
126  else
127  {
128  Info<< "Writing face centres: " << iopts.objectPath() << endl;
129  }
130  }
131 
132  // Like regIOobject::writeObject without instance() adaptation
133  // since this would write to e.g. 0/ instead of postProcessing/
134 
135  autoPtr<primitivePatch> ppPtr;
136 
137  {
138  OFstream os(iopts.objectPath(), streamOpt_);
139 
140  if (header_)
141  {
142  iopts.writeHeader(os);
143  }
144 
145  if (this->isPointData())
146  {
147  // Just like writeData, but without copying beforehand
148  os << points;
149  }
150  else
151  {
152  ppPtr.reset(new primitivePatch(SubList<face>(faces), points));
153 
154  // Just like writeData, but without copying beforehand
155  os << ppPtr().faceCentres();
156  }
157 
158  if (header_)
159  {
161  }
162  }
163 
164  if (writeNormal_ && !this->isPointData())
165  {
166  vectorIOField iofld
167  (
168  IOobject
169  (
170  iopts.objectPath().path()/"normal",
171  iopts.db(),
175  )
176  );
177  iofld.note() = "face data";
178 
179  OFstream os(iofld.objectPath(), streamOpt_);
180 
181  if (header_)
182  {
183  iofld.writeHeader(os);
184  }
185 
186  os << ppPtr().faceNormals();
187 
188  if (header_)
189  {
191  }
192  }
193 }
194 
195 
197 {
198  checkOpen();
199 
200  // Geometry: rootdir/surfaceName/"points"
201  // Field: rootdir/surfaceName/<TIME>/field
202 
203  fileName surfaceDir = outputPath_;
204 
205  // Dummy Time to use as objectRegistry
206  refPtr<Time> timePtr(Time::New(argList::envGlobalPath()));
207 
208  // const meshedSurf& surf = surface();
209  const meshedSurfRef& surf = adjustSurface();
210 
211  if (Pstream::master() || !parallel_)
212  {
213  if (!isDir(surfaceDir))
214  {
215  mkDir(surfaceDir);
216  }
217 
218  // Write sample locations
219  pointIOField iopts
220  (
221  IOobject
222  (
223  surfaceDir/"points",
224  *timePtr,
228  )
229  );
230  iopts.note() = (this->isPointData() ? "point data" : "face data");
231 
232  serialWriteGeometry(iopts, surf);
233  }
234 
235  wroteGeom_ = true;
236  return surfaceDir;
237 }
238 
239 
240 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241 
242 template<class Type>
243 Foam::fileName Foam::surfaceWriters::boundaryDataWriter::writeTemplate
244 (
245  const word& fieldName,
246  const Field<Type>& localValues
247 )
248 {
249  checkOpen();
250 
251  // Geometry: rootdir/surfaceName/"points"
252  // Field: rootdir/surfaceName/<TIME>/field
253 
254  fileName surfaceDir = outputPath_;
255 
256  const fileName outputFile(surfaceDir/timeName()/fieldName);
257 
258  // Implicit geometry merge()
259  tmp<Field<Type>> tfield = adjustField(fieldName, mergeField(localValues));
260 
261  if (verbose_)
262  {
263  Info<< " to " << outputFile << endl;
264  }
265 
266 
267  // Dummy Time to use as objectRegistry
269 
270  // const meshedSurf& surf = surface();
271  const meshedSurfRef& surf = adjustSurface();
272 
273  if (Pstream::master() || !parallel_)
274  {
275  if (!isDir(outputFile.path()))
276  {
277  mkDir(outputFile.path());
278  }
279 
280  // Write sample locations
281  {
282  pointIOField iopts
283  (
284  IOobject
285  (
286  surfaceDir/"points",
287  *timePtr,
291  )
292  );
293  iopts.note() = (this->isPointData() ? "point data" : "face data");
294 
295  serialWriteGeometry(iopts, surf);
296  }
297 
298  // Write field
299  {
300  IOField<Type> iofld
301  (
302  IOobject
303  (
304  outputFile,
305  *timePtr,
309  )
310  );
311  iofld.note() = (this->isPointData() ? "point data" : "face data");
312 
313  OFstream os(iofld.objectPath(), streamOpt_);
314 
315  if (header_)
316  {
317  iofld.writeHeader(os);
318  }
319 
320  // Just like writeData, but without copying beforehand
321  os << tfield();
322 
323  if (header_)
324  {
326  }
327  }
328  }
329 
330  wroteGeom_ = true;
331  return surfaceDir;
332 }
333 
334 
335 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
336 
337 // Field writing methods
339 
340 
341 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:71
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
vectorIOField pointIOField
pointIOField is a vectorIOField.
Definition: pointIOField.H:38
virtual void open(const fileName &outputPath)
Open for output on specified path, using existing surface.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
const string & note() const noexcept
Return the optional note.
Definition: IOobjectI.H:180
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:697
A simple container for options an IOstream can normally have.
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:169
Ignore writing from objectRegistry::writeObject()
bool writeHeader(Ostream &os) const
Write header with current type()
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:239
Macros for easy insertion into run-time selection tables.
Abstract definition of a meshed surface defined by faces and points.
Definition: meshedSurf.H:43
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:813
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
word timeName
Definition: getTimeIndex.H:3
virtual fileName write()
Write surface geometry to file.
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:567
const pointField & points
Implements a meshed surface by referencing another meshed surface or faces/points components...
Definition: meshedSurfRef.H:47
A class for handling words, derived from Foam::string.
Definition: word.H:63
const objectRegistry & db() const noexcept
Return the local objectRegistry.
Definition: IOobject.C:441
A surfaceWriter for outputting to a form usable for the timeVaryingMapped boundary condition...
virtual const faceList & faces() const =0
The faces used for the surface.
static Ostream & writeEndDivider(Ostream &os)
Write the standard end file divider.
Istream and Ostream manipulators taking arguments.
OBJstream os(runTime.globalPath()/outputName)
PrimitivePatch< SubList< face >, const pointField & > primitivePatch
A PrimitivePatch with a SubList addressing for the faces, const reference for the point field...
IOField< vector > vectorIOField
vectorField with IO.
Definition: vectorIOField.H:38
Convenience macros for instantiating surfaceWriter methods.
defineTypeName(abaqusWriter)
static fileName envGlobalPath()
Global case (directory) from environment variable.
Definition: argList.C:590
defineSurfaceWriterWriteFields(Foam::surfaceWriters::boundaryDataWriter)
static bool master(const label communicator=worldComm)
Am I the master rank.
Definition: UPstream.H:672
Nothing to be read.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:69
messageStream Info
Information stream (stdout output on master, null elsewhere)
Base class for surface writers.
A class for managing temporary objects.
Definition: HashPtrTable.H:50
addToRunTimeSelectionTable(surfaceWriter, abaqusWriter, word)
Do not request registration (bool: false)
virtual const pointField & points() const =0
The points used for the surface.
Namespace for OpenFOAM.