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-2020 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 {
49 }
50 }
51 
52 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
56 :
57  surfaceWriter(),
58  header_(true),
59  streamOpt_()
60 {}
61 
62 
64 (
65  const dictionary& options
66 )
67 :
68  surfaceWriter(options),
69  header_(options.getOrDefault("header", true)),
70  streamOpt_
71  (
72  IOstream::formatEnum("format", options, IOstream::ASCII),
73  IOstream::compressionEnum("compression", options)
74  )
75 {}
76 
77 
79 (
80  const meshedSurf& surf,
81  const fileName& outputPath,
82  bool parallel,
83  const dictionary& options
84 )
85 :
86  boundaryDataWriter(options)
87 {
88  open(surf, outputPath, parallel);
89 }
90 
91 
93 (
94  const pointField& points,
95  const faceList& faces,
96  const fileName& outputPath,
97  bool parallel,
98  const dictionary& options
99 )
100 :
101  boundaryDataWriter(options)
102 {
103  open(points, faces, outputPath, parallel);
104 }
105 
106 
107 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
108 
110 {
111  checkOpen();
112 
113  // Geometry: rootdir/surfaceName/"points"
114  // Field: rootdir/surfaceName/<TIME>/field
115 
116  fileName surfaceDir = outputPath_;
117 
118  // Dummy Time to use as objectRegistry
120 
121  const meshedSurf& surf = surface();
122 
123  if (Pstream::master() || !parallel_)
124  {
125  if (!isDir(surfaceDir))
126  {
127  mkDir(surfaceDir);
128  }
129 
130  pointIOField iopts
131  (
132  IOobject
133  (
134  surfaceDir/"points",
135  *dummyTimePtr,
138  false
139  )
140  );
141 
142  if (verbose_)
143  {
144  Info<< "Writing points: " << iopts.objectPath() << endl;
145  }
146 
147 
148  // Like regIOobject::writeObject without instance() adaptation
149  // since this would write to e.g. 0/ instead of postProcessing/
150 
151  OFstream osGeom(iopts.objectPath(), streamOpt_);
152 
153  if (header_)
154  {
155  iopts.writeHeader(osGeom);
156  }
157 
158  // Just like writeData, but without copying beforehand
159  osGeom << surf.points();
160 
161  if (header_)
162  {
163  iopts.writeEndDivider(osGeom);
164  }
165  }
166 
167  wroteGeom_ = true;
168  return surfaceDir;
169 }
170 
171 
172 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
173 
174 template<class Type>
175 Foam::fileName Foam::surfaceWriters::boundaryDataWriter::writeTemplate
176 (
177  const word& fieldName,
178  const Field<Type>& localValues
179 )
180 {
181  checkOpen();
182 
183  // Geometry: rootdir/surfaceName/"points"
184  // Field: rootdir/surfaceName/<TIME>/field
185 
186  fileName surfaceDir = outputPath_;
187 
188  const fileName outputFile(surfaceDir/timeName()/fieldName);
189 
190 
191  // Dummy Time to use as objectRegistry
193 
194 
195  // Geometry merge() implicit
196  tmp<Field<Type>> tfield = mergeField(localValues);
197 
198  const meshedSurf& surf = surface();
199 
200  if (Pstream::master() || !parallel_)
201  {
202  const pointField& points = surf.points();
203  const faceList& faces = surf.faces();
204 
205  if (!isDir(outputFile.path()))
206  {
207  mkDir(outputFile.path());
208  }
209 
210  pointIOField iopts
211  (
212  IOobject
213  (
214  surfaceDir/"points",
215  *dummyTimePtr,
218  false
219  )
220  );
221 
222  if (verbose_)
223  {
224  if (this->isPointData())
225  {
226  Info<< "Writing points: " << iopts.objectPath() << endl;
227  }
228  else
229  {
230  Info<< "Writing face centres: " << iopts.objectPath() << endl;
231  }
232  }
233 
234  // Like regIOobject::writeObject without instance() adaptation
235  // since this would write to e.g. 0/ instead of postProcessing/
236 
237  OFstream osGeom(iopts.objectPath(), streamOpt_);
238 
239  if (header_)
240  {
241  iopts.writeHeader(osGeom);
242  }
243 
244  if (this->isPointData())
245  {
246  // Just like writeData, but without copying beforehand
247  osGeom << points;
248  }
249  else
250  {
251  primitivePatch pp(SubList<face>(faces), points);
252 
253  // Just like writeData, but without copying beforehand
254  osGeom << pp.faceCentres();
255  }
256 
257  if (header_)
258  {
259  iopts.writeEndDivider(osGeom);
260  }
261 
262 
263  // Write field
264  {
265  IOField<Type> iofld
266  (
267  IOobject
268  (
269  outputFile,
270  *dummyTimePtr,
273  false
274  )
275  );
276 
277  OFstream osField(iofld.objectPath(), streamOpt_);
278 
279  if (header_)
280  {
281  iofld.writeHeader(osField);
282  }
283 
284  // Just like writeData, but without copying beforehand
285  osField << tfield();
286 
287  if (header_)
288  {
289  iofld.writeEndDivider(osField);
290  }
291  }
292  }
293 
294  wroteGeom_ = true;
295  return surfaceDir;
296 }
297 
298 
299 // Field writing methods
301 
302 
303 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
boundaryDataSurfaceWriter.H
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::surfaceWriter
Base class for surface writers.
Definition: surfaceWriter.H:111
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::argList::envGlobalPath
static fileName envGlobalPath()
Global case (directory) from environment variable.
Definition: argList.C:523
Foam::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:186
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
Foam::surfaceWriters::boundaryDataWriter
A surfaceWriter for outputting to a form usable for the timeVaryingMapped boundary condition....
Definition: boundaryDataSurfaceWriter.H:158
Foam::meshedSurf::faces
virtual const faceList & faces() const =0
The faces used for the surface.
Foam::meshedSurf
Abstract definition of a meshed surface defined by faces and points.
Definition: meshedSurf.H:49
Foam::surfaceWriters::defineTypeName
defineTypeName(boundaryDataWriter)
pointIOField.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
surfaceWriterMethods.H
Convenience macros for instantiating surfaceWriter methods.
Foam::surfaceWriters::boundaryDataWriter::boundaryDataWriter
boundaryDataWriter()
Default construct.
Definition: boundaryDataSurfaceWriter.C:55
OFstream.H
Foam::Field< vector >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
argList.H
IOmanip.H
Istream and Ostream manipulators taking arguments.
Foam::meshedSurf::points
virtual const pointField & points() const =0
The points used for the surface.
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::IOstreamOption::compressionEnum
static compressionType compressionEnum(const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
The compression enum corresponding to the string.
Definition: IOstreamOption.C:92
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
defineSurfaceWriterWriteFields
defineSurfaceWriterWriteFields(Foam::surfaceWriters::boundaryDataWriter)
Foam::Time::New
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:722
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:87
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:439
Foam::surfaceWriters::boundaryDataWriter::write
virtual fileName write()
Write surface geometry to file.
Definition: boundaryDataSurfaceWriter.C:109
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, boundaryDataWriter, word)
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::IOstreamOption::formatEnum
static streamFormat formatEnum(const word &formatName, const streamFormat deflt=streamFormat::ASCII)
Definition: IOstreamOption.C:53
Foam::List< face >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:122
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:507
primitivePatch.H
Foam::PrimitivePatch::faceCentres
const Field< point_type > & faceCentres() const
Return face centres for patch.
Definition: PrimitivePatch.C:380
Foam::isDir
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:643
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:85