foamSurfaceWriter.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) 2011-2016 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 
29 #include "foamSurfaceWriter.H"
30 #include "OFstream.H"
31 #include "OSspecific.H"
32 #include "surfaceWriterMethods.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace surfaceWriters
40 {
41  defineTypeName(foamWriter);
42  addToRunTimeSelectionTable(surfaceWriter, foamWriter, word);
43  addToRunTimeSelectionTable(surfaceWriter, foamWriter, wordDict);
44 }
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
51 :
52  surfaceWriter(),
53  streamOpt_(),
54  fieldScale_()
55 {}
56 
57 
59 (
60  const dictionary& options
61 )
62 :
63  surfaceWriter(options),
64  streamOpt_
65  (
66  IOstream::formatEnum("format", options, IOstream::ASCII),
67  IOstream::compressionEnum("compression", options)
68  ),
69  fieldScale_(options.subOrEmptyDict("fieldScale"))
70 {}
71 
72 
74 (
75  const meshedSurf& surf,
76  const fileName& outputPath,
77  bool parallel,
78  const dictionary& options
79 )
80 :
81  foamWriter(options)
82 {
83  open(surf, outputPath, parallel);
84 }
85 
86 
88 (
89  const pointField& points,
90  const faceList& faces,
91  const fileName& outputPath,
92  bool parallel,
93  const dictionary& options
94 )
95 :
96  foamWriter(options)
97 {
98  open(points, faces, outputPath, parallel);
99 }
100 
101 
102 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
103 
105 {
106  checkOpen();
107 
108  // Geometry:
109  // - rootdir/<TIME>/surfaceName/{points,faces,faceCentres}
110 
111  fileName surfaceDir = outputPath_;
112  if (useTimeDir() && !timeName().empty())
113  {
114  // Splice in time-directory
115  surfaceDir = outputPath_.path() / timeName() / outputPath_.name();
116  }
117 
118  if (verbose_)
119  {
120  Info<< "Writing geometry to " << surfaceDir << endl;
121  }
122 
123 
124  const meshedSurf& surf = surface();
125 
126  if (Pstream::master() || !parallel_)
127  {
128  const pointField& points = surf.points();
129  const faceList& faces = surf.faces();
130 
131  if (!isDir(surfaceDir))
132  {
133  mkDir(surfaceDir);
134  }
135 
136  // Points
137  OFstream(surfaceDir/"points", streamOpt_)() << points;
138 
139  // Faces
140  OFstream(surfaceDir/"faces", streamOpt_)() << faces;
141 
142  // Face centers.
143  // Not really necessary but very handy when reusing as inputs
144  // for e.g. timeVaryingMapped bc.
145  pointField faceCentres(faces.size(), Zero);
146 
147  forAll(faces, facei)
148  {
149  faceCentres[facei] = faces[facei].centre(points);
150  }
151 
152  OFstream(surfaceDir/"faceCentres", streamOpt_)() << faceCentres;
153  }
154 
155  wroteGeom_ = true;
156  return surfaceDir;
157 }
158 
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 template<class Type>
163 Foam::fileName Foam::surfaceWriters::foamWriter::writeTemplate
164 (
165  const word& fieldName,
166  const Field<Type>& localValues
167 )
168 {
169  // Separate geometry
170  if (!wroteGeom_)
171  {
172  write();
173  }
174 
175  checkOpen();
176 
177  // Geometry should already have been written
178  // Values to separate directory (e.g. "scalarField/p")
179 
180  // Field: rootdir/<TIME>/surfaceName/fieldType/field
181  //?? -> or rootdir/surfaceName/fieldType/<TIME>/field
182 
183  fileName surfaceDir = outputPath_;
184  if (useTimeDir() && !timeName().empty())
185  {
186  // Splice in time-directory
187  surfaceDir = outputPath_.path() / timeName() / outputPath_.name();
188  }
189 
190  const fileName outputFile
191  (
192  surfaceDir
194  / fieldName
195  );
196 
197 
198  // Output scaling for the variable, but not for integer types.
199  // could also solve with clever templating
200 
201  const scalar varScale =
202  (
203  std::is_integral<Type>::value
204  ? scalar(1)
205  : fieldScale_.getOrDefault<scalar>(fieldName, 1)
206  );
207 
208  if (verbose_)
209  {
210  Info<< "Writing field " << fieldName;
211  if (!equal(varScale, 1))
212  {
213  Info<< " (scaling " << varScale << ')';
214  }
215  Info<< " to " << surfaceDir << endl;
216  }
217 
218 
219  // Implicit geometry merge()
220  tmp<Field<Type>> tfield = mergeField(localValues) * varScale;
221 
222  if (Pstream::master())
223  {
224  if (!isDir(outputFile.path()))
225  {
226  mkDir(outputFile.path());
227  }
228 
229  // Write field
230  OFstream(outputFile, streamOpt_)() << tfield();
231  }
232 
233  wroteGeom_ = true;
234  return outputFile;
235 }
236 
237 
238 // Field writing methods
240 
241 
242 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::surfaceWriters::foamWriter::write
virtual fileName write()
Write surface geometry to file.
Definition: foamSurfaceWriter.C:104
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::surfaceWriters::defineTypeName
defineTypeName(abaqusWriter)
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::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
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
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
surfaceWriterMethods.H
Convenience macros for instantiating surfaceWriter methods.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::surfaceWriters::foamWriter
A surfaceWriter for OpenFOAM surfaces.
Definition: foamSurfaceWriter.H:125
OFstream.H
Foam::surfaceWriters::foamWriter::foamWriter
foamWriter()
Default construct.
Definition: foamSurfaceWriter.C:50
Foam::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, abaqusWriter, word)
Foam::Field< vector >
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
foamSurfaceWriter.H
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
Foam::dictionary::subOrEmptyDict
dictionary subOrEmptyDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX, const bool mandatory=false) const
Definition: dictionary.C:540
Foam::FieldBase::typeName
static const char *const typeName
Typename for Field.
Definition: FieldBase.H:59
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
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 >
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
defineSurfaceWriterWriteFields
defineSurfaceWriterWriteFields(Foam::surfaceWriters::foamWriter)
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
Foam::equal
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46
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