starcdSurfaceWriter.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 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 "starcdSurfaceWriter.H"
30 #include "OFstream.H"
31 #include "OSspecific.H"
32 #include "MeshedSurfaceProxy.H"
33 #include "surfaceWriterMethods.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 namespace surfaceWriters
41 {
42  defineTypeName(starcdWriter);
43  addToRunTimeSelectionTable(surfaceWriter, starcdWriter, word);
44  addToRunTimeSelectionTable(surfaceWriter, starcdWriter, wordDict);
45 }
46 }
47 
48 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52  // Emit each component
53  template<class Type>
54  static inline void writeData(Ostream& os, const Type& val)
55  {
56  for (direction cmpt=0; cmpt < pTraits<Type>::nComponents; ++cmpt)
57  {
58  os << ' ' << component(val, cmpt);
59  }
60  os << nl;
61  }
62 
63 } // End namespace Foam
64 
65 
66 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
67 
69 :
70  surfaceWriter(),
71  streamOpt_()
72 {}
73 
74 
76 (
77  const dictionary& options
78 )
79 :
80  surfaceWriter(options),
81  streamOpt_
82  (
84  IOstream::compressionEnum("compression", options)
85  )
86 {}
87 
88 
90 (
91  const meshedSurf& surf,
92  const fileName& outputPath,
93  bool parallel,
94  const dictionary& options
95 )
96 :
97  starcdWriter(options)
98 {
99  open(surf, outputPath, parallel);
100 }
101 
102 
104 (
105  const pointField& points,
106  const faceList& faces,
107  const fileName& outputPath,
108  bool parallel,
109  const dictionary& options
110 )
111 :
112  starcdWriter(options)
113 {
114  open(points, faces, outputPath, parallel);
115 }
116 
117 
118 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
119 
121 {
122  checkOpen();
123 
124  // Geometry: rootdir/<TIME>/surfaceName.{inp,cel,vrt}
125 
126  fileName outputFile = outputPath_;
127  if (useTimeDir() && !timeName().empty())
128  {
129  // Splice in time-directory
130  outputFile = outputPath_.path() / timeName() / outputPath_.name();
131  }
132  outputFile.ext("inp");
133 
134  if (verbose_)
135  {
136  Info<< "Writing geometry to " << outputFile << endl;
137  }
138 
139  const meshedSurf& surf = surface();
140 
141  if (Pstream::master() || !parallel_)
142  {
143  if (!isDir(outputFile.path()))
144  {
145  mkDir(outputFile.path());
146  }
147 
149  (
150  surf.points(),
151  surf.faces(),
152  UList<surfZone>::null(), // one zone
153  labelUList::null(), // no faceMap
154  surf.faceIds() // with face ids (if possible)
155  ).write
156  (
157  outputFile,
158  "starcd", // Canonical selection name
159  streamOpt_
160  );
161  }
162 
163  wroteGeom_ = true;
164  return outputFile;
165 }
166 
167 
168 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
169 
170 template<class Type>
171 Foam::fileName Foam::surfaceWriters::starcdWriter::writeTemplate
172 (
173  const word& fieldName,
174  const Field<Type>& localValues
175 )
176 {
177  // Separate geometry
178  if (!wroteGeom_)
179  {
180  write();
181  }
182 
183  checkOpen();
184 
185  // Field: rootdir/<TIME>/<field>_surfaceName.usr
186 
187  fileName outputFile = outputPath_.path();
188  if (useTimeDir() && !timeName().empty())
189  {
190  // Splice in time-directory
191  outputFile /= timeName();
192  }
193 
194  // Append <field>_surfaceName.usr
195  outputFile /= fieldName + '_' + outputPath_.name();
196  outputFile.ext("usr");
197 
198  if (verbose_)
199  {
200  Info<< "Writing field " << fieldName << " to " << outputFile << endl;
201  }
202 
203 
204  // geometry merge() implicit
205  tmp<Field<Type>> tfield = mergeField(localValues);
206 
207  if (Pstream::master() || !parallel_)
208  {
209  const auto& values = tfield();
210 
211  if (!isDir(outputFile.path()))
212  {
213  mkDir(outputFile.path());
214  }
215 
216  OFstream os(outputFile, streamOpt_);
217 
218  // 1-based ids
219  label elemId = 1;
220 
221  // No header, just write values
222  for (const Type& val : values)
223  {
224  os << elemId;
225  writeData(os, val);
226 
227  ++elemId;
228  }
229  }
230 
231  wroteGeom_ = true;
232  return outputFile;
233 }
234 
235 
236 // Field writing methods
238 
239 
240 // ************************************************************************* //
Foam::surfaceWriters::starcdWriter::write
virtual fileName write()
Write surface geometry to file.
Definition: starcdSurfaceWriter.C:120
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
MeshedSurfaceProxy.H
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:44
starcdSurfaceWriter.H
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::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
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:350
surfaceWriterMethods.H
Convenience macros for instantiating surfaceWriter methods.
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:82
OFstream.H
Foam::Field< vector >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
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::writeData
static void writeData(Ostream &os, const Type &val)
Definition: rawSurfaceWriterImpl.C:39
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::fileName::ext
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:228
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::surfaceWriters::starcdWriter::starcdWriter
starcdWriter()
Default construct.
Definition: starcdSurfaceWriter.C:68
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::meshedSurf::faceIds
virtual const labelList & faceIds() const
Per-face identifier (eg, element Id)
Definition: meshedSurf.H:83
Foam::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, boundaryDataWriter, word)
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List< face >
Foam::surfaceWriters::starcdWriter
A surfaceWriter for STARCD files.
Definition: starcdSurfaceWriter.H:103
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::direction
uint8_t direction
Definition: direction.H:47
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
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::UList::null
static const UList< T > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:54
defineSurfaceWriterWriteFields
defineSurfaceWriterWriteFields(Foam::surfaceWriters::starcdWriter)
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