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-2021 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);
48  addToRunTimeSelectionTable(surfaceWriter, boundaryDataWriter, wordDict);
49 }
50 }
51 
52 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
56 :
57  surfaceWriter(),
58  header_(true),
59  streamOpt_(),
60  fieldScale_()
61 {}
62 
63 
65 (
66  const dictionary& options
67 )
68 :
69  surfaceWriter(options),
70  header_(options.getOrDefault("header", true)),
71  streamOpt_
72  (
73  IOstream::formatEnum("format", options, IOstream::ASCII),
74  IOstream::compressionEnum("compression", options)
75  ),
76  fieldScale_(options.subOrEmptyDict("fieldScale"))
77 {}
78 
79 
81 (
82  const meshedSurf& surf,
83  const fileName& outputPath,
84  bool parallel,
85  const dictionary& options
86 )
87 :
88  boundaryDataWriter(options)
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  OFstream osGeom(iopts.objectPath(), streamOpt_);
136 
137  if (header_)
138  {
139  iopts.writeHeader(osGeom);
140  }
141 
142  if (this->isPointData())
143  {
144  // Just like writeData, but without copying beforehand
145  osGeom << points;
146  }
147  else
148  {
149  primitivePatch pp(SubList<face>(faces), points);
150 
151  // Just like writeData, but without copying beforehand
152  osGeom << pp.faceCentres();
153  }
154 
155  if (header_)
156  {
157  iopts.writeEndDivider(osGeom);
158  }
159 }
160 
161 
163 {
164  checkOpen();
165 
166  // Geometry: rootdir/surfaceName/"points"
167  // Field: rootdir/surfaceName/<TIME>/field
168 
169  fileName surfaceDir = outputPath_;
170 
171  // Dummy Time to use as objectRegistry
173 
174  const meshedSurf& surf = surface();
175 
176  if (Pstream::master() || !parallel_)
177  {
178  if (!isDir(surfaceDir))
179  {
180  mkDir(surfaceDir);
181  }
182 
183  // Write sample locations
184  pointIOField iopts
185  (
186  IOobject
187  (
188  surfaceDir/"points",
189  *dummyTimePtr,
192  false
193  )
194  );
195  iopts.note() = (this->isPointData() ? "point data" : "face data");
196 
197  serialWriteGeometry(iopts, surf);
198  }
199 
200  wroteGeom_ = true;
201  return surfaceDir;
202 }
203 
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 template<class Type>
208 Foam::fileName Foam::surfaceWriters::boundaryDataWriter::writeTemplate
209 (
210  const word& fieldName,
211  const Field<Type>& localValues
212 )
213 {
214  checkOpen();
215 
216  // Geometry: rootdir/surfaceName/"points"
217  // Field: rootdir/surfaceName/<TIME>/field
218 
219  fileName surfaceDir = outputPath_;
220 
221  const fileName outputFile(surfaceDir/timeName()/fieldName);
222 
223 
224  // Output scaling for the variable, but not for integer types.
225  // could also solve with clever templating
226 
227  const scalar varScale =
228  (
229  std::is_integral<Type>::value
230  ? scalar(1)
231  : fieldScale_.getOrDefault<scalar>(fieldName, 1)
232  );
233 
234 
235  // Dummy Time to use as objectRegistry
237 
238 
239  // Implicit geometry merge()
240  tmp<Field<Type>> tfield = mergeField(localValues) * varScale;
241 
242  const meshedSurf& surf = surface();
243 
244  if (Pstream::master() || !parallel_)
245  {
246  if (!isDir(outputFile.path()))
247  {
248  mkDir(outputFile.path());
249  }
250 
251  // Write sample locations
252  {
253  pointIOField iopts
254  (
255  IOobject
256  (
257  surfaceDir/"points",
258  *dummyTimePtr,
261  false
262  )
263  );
264  iopts.note() = (this->isPointData() ? "point data" : "face data");
265 
266  serialWriteGeometry(iopts, surf);
267  }
268 
269  // Write field
270  {
271  IOField<Type> iofld
272  (
273  IOobject
274  (
275  outputFile,
276  *dummyTimePtr,
279  false
280  )
281  );
282  iofld.note() = (this->isPointData() ? "point data" : "face data");
283 
284  OFstream osField(iofld.objectPath(), streamOpt_);
285 
286  if (header_)
287  {
288  iofld.writeHeader(osField);
289  }
290 
291  // Just like writeData, but without copying beforehand
292  osField << tfield();
293 
294  if (header_)
295  {
296  iofld.writeEndDivider(osField);
297  }
298  }
299  }
300 
301  wroteGeom_ = true;
302  return surfaceDir;
303 }
304 
305 
306 // Field writing methods
308 
309 
310 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
boundaryDataSurfaceWriter.H
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
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:65
Foam::surfaceWriter
Base class for surface writers.
Definition: surfaceWriter.H:114
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::surfaceWriters::defineTypeName
defineTypeName(abaqusWriter)
Foam::argList::envGlobalPath
static fileName envGlobalPath()
Global case (directory) from environment variable.
Definition: argList.C:549
Foam::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::IOobject::writeEndDivider
static Ostream & writeEndDivider(Ostream &os)
Write the standard end file divider.
Definition: IOobjectWriteHeader.C:142
Foam::surfaceWriters::boundaryDataWriter
A surfaceWriter for outputting to a form usable for the timeVaryingMapped boundary condition....
Definition: boundaryDataSurfaceWriter.H:172
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::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
pointIOField.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
surfaceWriterMethods.H
Convenience macros for instantiating surfaceWriter methods.
Foam::surfaceWriters::boundaryDataWriter::boundaryDataWriter
boundaryDataWriter()
Default construct.
Definition: boundaryDataSurfaceWriter.C:55
OFstream.H
Foam::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, abaqusWriter, word)
Foam::Field< vector >
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
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:123
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:717
Foam::dictionary::subOrEmptyDict
dictionary subOrEmptyDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX, const bool mandatory=false) const
Definition: dictionary.C:540
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::surfaceWriters::boundaryDataWriter::write
virtual fileName write()
Write surface geometry to file.
Definition: boundaryDataSurfaceWriter.C:162
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
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::primitivePatch
PrimitivePatch< SubList< face >, const pointField & > primitivePatch
A PrimitivePatch with a SubList addressing for the faces, const reference for the point field.
Definition: primitivePatch.H:51
Foam::IOobject::writeHeader
bool writeHeader(Ostream &os) const
Write header with current type()
Definition: IOobjectWriteHeader.C:277
Foam::IOobject::objectPath
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:214
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
Foam::IOobject::NO_READ
Definition: IOobject.H:188
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::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