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-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 "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 }
45 }
46 
47 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51  // Emit each component
52  template<class Type>
53  static inline void writeData(Ostream& os, const Type& val)
54  {
56  for (direction cmpt=0; cmpt < ncmpt; ++cmpt)
57  {
58  os << ' ' << component(val, cmpt);
59  }
60  os << nl;
61  }
62 
63 } // End namespace Foam
64 
65 
66 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
67 
69 :
71 {}
72 
73 
75 (
76  const dictionary& options
77 )
78 :
79  surfaceWriter(options)
80 {}
81 
82 
84 (
85  const meshedSurf& surf,
86  const fileName& outputPath,
87  bool parallel,
88  const dictionary& options
89 )
90 :
91  starcdWriter(options)
92 {
93  open(surf, outputPath, parallel);
94 }
95 
96 
98 (
99  const pointField& points,
100  const faceList& faces,
101  const fileName& outputPath,
102  bool parallel,
103  const dictionary& options
104 )
105 :
106  starcdWriter(options)
107 {
108  open(points, faces, outputPath, parallel);
109 }
110 
111 
112 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113 
115 {
116  checkOpen();
117 
118  // Geometry: rootdir/<TIME>/surfaceName.{inp,cel,vrt}
119 
120  fileName outputFile = outputPath_;
121  if (useTimeDir() && !timeName().empty())
122  {
123  // Splice in time-directory
124  outputFile = outputPath_.path() / timeName() / outputPath_.name();
125  }
126  outputFile.ext("inp");
127 
128  if (verbose_)
129  {
130  Info<< "Writing geometry to " << outputFile << endl;
131  }
132 
133  const meshedSurf& surf = surface();
134 
135  if (Pstream::master() || !parallel_)
136  {
137  if (!isDir(outputFile.path()))
138  {
139  mkDir(outputFile.path());
140  }
141 
143  (
144  surf.points(),
145  surf.faces()
146  ).write(outputFile, "inp");
147  }
148 
149  wroteGeom_ = true;
150  return outputFile;
151 }
152 
153 
154 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
155 
156 template<class Type>
157 Foam::fileName Foam::surfaceWriters::starcdWriter::writeTemplate
158 (
159  const word& fieldName,
160  const Field<Type>& localValues
161 )
162 {
163  // Separate geometry
164  if (!wroteGeom_)
165  {
166  write();
167  }
168 
169  checkOpen();
170 
171  // Field: rootdir/<TIME>/<field>_surfaceName.usr
172 
173  fileName outputFile = outputPath_.path();
174  if (useTimeDir() && !timeName().empty())
175  {
176  // Splice in time-directory
177  outputFile /= timeName();
178  }
179 
180  // Append <field>_surfaceName.usr
181  outputFile /= fieldName + '_' + outputPath_.name();
182  outputFile.ext("usr");
183 
184  if (verbose_)
185  {
186  Info<< "Writing field " << fieldName << " to " << outputFile << endl;
187  }
188 
189 
190  // geometry merge() implicit
191  tmp<Field<Type>> tfield = mergeField(localValues);
192 
193  if (Pstream::master() || !parallel_)
194  {
195  const auto& values = tfield();
196 
197  if (!isDir(outputFile.path()))
198  {
199  mkDir(outputFile.path());
200  }
201 
202  OFstream os(outputFile);
203 
204  // 1-based ids
205  label elemId = 1;
206 
207  // No header, just write values
208  for (const Type& val : values)
209  {
210  os << elemId;
211  writeData(os, val);
212 
213  ++elemId;
214  }
215  }
216 
217  wroteGeom_ = true;
218  return outputFile;
219 }
220 
221 
222 // Field writing methods
224 
225 
226 // ************************************************************************* //
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::surfaceWriters::starcdWriter::write
virtual fileName write()
Write surface geometry to file.
Definition: starcdSurfaceWriter.C:114
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:337
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:78
OFstream.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
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::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()
Construct null.
Definition: starcdSurfaceWriter.C:68
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::nl
constexpr char nl
Definition: Ostream.H:372
Foam::List< face >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:52
Foam::surfaceWriters::starcdWriter
A surfaceWriter for STARCD files.
Definition: starcdSurfaceWriter.H:87
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
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