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-2019 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 }
44 }
45 
46 
47 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
48 
50 :
52 {}
53 
54 
56 (
57  const dictionary& options
58 )
59 :
60  surfaceWriter(options)
61 {}
62 
63 
65 (
66  const meshedSurf& surf,
67  const fileName& outputPath,
68  bool parallel,
69  const dictionary& options
70 )
71 :
72  foamWriter(options)
73 {
74  open(surf, outputPath, parallel);
75 }
76 
77 
79 (
80  const pointField& points,
81  const faceList& faces,
82  const fileName& outputPath,
83  bool parallel,
84  const dictionary& options
85 )
86 :
87  foamWriter(options)
88 {
89  open(points, faces, outputPath, parallel);
90 }
91 
92 
93 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
94 
96 {
97  checkOpen();
98 
99  // Geometry:
100  // - rootdir/<TIME>/surfaceName/{points,faces}
101 
102  fileName surfaceDir = outputPath_;
103  if (useTimeDir() && !timeName().empty())
104  {
105  // Splice in time-directory
106  surfaceDir = outputPath_.path() / timeName() / outputPath_.name();
107  }
108 
109  if (verbose_)
110  {
111  Info<< "Writing geometry to " << surfaceDir << endl;
112  }
113 
114 
115  const meshedSurf& surf = surface();
116 
117  if (Pstream::master() || !parallel_)
118  {
119  const pointField& points = surf.points();
120  const faceList& faces = surf.faces();
121 
122  if (!isDir(surfaceDir))
123  {
124  mkDir(surfaceDir);
125  }
126 
127  // Points
128  OFstream(surfaceDir/"points")() << points;
129 
130  // Faces
131  OFstream(surfaceDir/"faces")() << faces;
132 
133  // Face centers.
134  // Not really necessary but very handy when reusing as inputs
135  // for e.g. timeVaryingMapped bc.
136  pointField faceCentres(faces.size(), Zero);
137 
138  forAll(faces, facei)
139  {
140  faceCentres[facei] = faces[facei].centre(points);
141  }
142 
143  OFstream(surfaceDir/"faceCentres")() << faceCentres;
144  }
145 
146  wroteGeom_ = true;
147  return surfaceDir;
148 }
149 
150 
151 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
152 
153 template<class Type>
154 Foam::fileName Foam::surfaceWriters::foamWriter::writeTemplate
155 (
156  const word& fieldName,
157  const Field<Type>& localValues
158 )
159 {
160  // Separate geometry
161  if (!wroteGeom_)
162  {
163  write();
164  }
165 
166  checkOpen();
167 
168  // Geometry should already have been written
169  // Values to separate directory (e.g. "scalarField/p")
170 
171  // Field: rootdir/<TIME>/surfaceName/fieldType/field
172  //?? -> or rootdir/surfaceName/fieldType/<TIME>/field
173 
174  fileName surfaceDir = outputPath_;
175  if (useTimeDir() && !timeName().empty())
176  {
177  // Splice in time-directory
178  surfaceDir = outputPath_.path() / timeName() / outputPath_.name();
179  }
180 
181  const fileName outputFile
182  (
183  surfaceDir
185  / fieldName
186  );
187 
188  if (verbose_)
189  {
190  Info<< "Writing field " << fieldName << " to " << surfaceDir << endl;
191  }
192 
193 
194  // geometry merge() implicit
195  tmp<Field<Type>> tfield = mergeField(localValues);
196 
197  if (Pstream::master())
198  {
199  if (!isDir(outputFile.path()))
200  {
201  mkDir(outputFile.path());
202  }
203 
204  // Write field
205  OFstream(outputFile)() << tfield();
206  }
207 
208  wroteGeom_ = true;
209  return outputFile;
210 }
211 
212 
213 // Field writing methods
215 
216 
217 // ************************************************************************* //
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:95
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::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::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
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)
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
surfaceWriterMethods.H
Convenience macros for instantiating surfaceWriter methods.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::surfaceWriters::foamWriter
A surfaceWriter for OpenFOAM surfaces.
Definition: foamSurfaceWriter.H:81
OFstream.H
Foam::surfaceWriters::foamWriter::foamWriter
foamWriter()
Construct null.
Definition: foamSurfaceWriter.C:49
Foam::Field< vector >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
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::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
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:99
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, boundaryDataWriter, word)
Foam::List< face >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:52
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:35
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::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