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 "ListOps.H"
31 #include "OFstream.H"
32 #include "OSspecific.H"
33 #include "MeshedSurfaceProxy.H"
34 #include "surfaceWriterMethods.H"
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41 namespace surfaceWriters
42 {
43  defineTypeName(starcdWriter);
44  addToRunTimeSelectionTable(surfaceWriter, starcdWriter, word);
45  addToRunTimeSelectionTable(surfaceWriter, starcdWriter, wordDict);
46 }
47 }
48 
49 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53  // Emit each component
54  template<class Type>
55  static inline void writeData(Ostream& os, const Type& val)
56  {
57  for (direction cmpt=0; cmpt < pTraits<Type>::nComponents; ++cmpt)
58  {
59  os << ' ' << component(val, cmpt);
60  }
61  os << nl;
62  }
63 
64 } // End namespace Foam
65 
66 
67 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
68 
70 :
72  streamOpt_(),
73  fieldScale_()
74 {}
75 
76 
78 (
79  const dictionary& options
80 )
81 :
82  surfaceWriter(options),
83  streamOpt_
84  (
85  IOstream::ASCII,
86  IOstream::compressionEnum("compression", options)
87  ),
88  fieldScale_(options.subOrEmptyDict("fieldScale"))
89 {}
90 
91 
93 (
94  const meshedSurf& surf,
95  const fileName& outputPath,
96  bool parallel,
97  const dictionary& options
98 )
99 :
100  starcdWriter(options)
101 {
102  open(surf, outputPath, parallel);
103 }
104 
105 
107 (
108  const pointField& points,
109  const faceList& faces,
110  const fileName& outputPath,
111  bool parallel,
112  const dictionary& options
113 )
114 :
115  starcdWriter(options)
116 {
117  open(points, faces, outputPath, parallel);
118 }
119 
120 
121 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
122 
124 {
125  checkOpen();
126 
127  // Geometry: rootdir/<TIME>/surfaceName.{inp,cel,vrt}
128 
129  fileName outputFile = outputPath_;
130  if (useTimeDir() && !timeName().empty())
131  {
132  // Splice in time-directory
133  outputFile = outputPath_.path() / timeName() / outputPath_.name();
134  }
135  outputFile.ext("inp");
136 
137  if (verbose_)
138  {
139  Info<< "Writing geometry to " << outputFile << endl;
140  }
141 
142  const meshedSurf& surf = surface();
143 
144  if (Pstream::master() || !parallel_)
145  {
146  if (!isDir(outputFile.path()))
147  {
148  mkDir(outputFile.path());
149  }
150 
151  const labelUList& origFaceIds = surf.faceIds();
152 
153  // Face ids (if possible)
154  const labelUList& elemIds =
155  (
156  !ListOps::found(origFaceIds, lessOp1<label>(0))
157  ? origFaceIds
158  : labelUList::null()
159  );
160 
161  MeshedSurfaceProxy<face>
162  (
163  surf.points(),
164  surf.faces(),
165  UList<surfZone>::null(), // one zone
166  labelUList::null(), // no faceMap
167  elemIds // face ids
168  ).write
169  (
170  outputFile,
171  "starcd", // Canonical selection name
172  streamOpt_
173  );
174  }
175 
176  wroteGeom_ = true;
177  return outputFile;
178 }
179 
180 
181 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
182 
183 template<class Type>
184 Foam::fileName Foam::surfaceWriters::starcdWriter::writeTemplate
185 (
186  const word& fieldName,
187  const Field<Type>& localValues
188 )
189 {
190  // Separate geometry
191  if (!wroteGeom_)
192  {
193  write();
194  }
195 
196  checkOpen();
197 
198  // Field: rootdir/<TIME>/<field>_surfaceName.usr
199 
200  fileName outputFile = outputPath_.path();
201  if (useTimeDir() && !timeName().empty())
202  {
203  // Splice in time-directory
204  outputFile /= timeName();
205  }
206 
207  // Append <field>_surfaceName.usr
208  outputFile /= fieldName + '_' + outputPath_.name();
209  outputFile.ext("usr");
210 
211 
212  // Output scaling for the variable, but not for integer types.
213  // could also solve with clever templating
214 
215  const scalar varScale =
216  (
217  std::is_integral<Type>::value
218  ? scalar(1)
219  : fieldScale_.getOrDefault<scalar>(fieldName, 1)
220  );
221 
222  if (verbose_)
223  {
224  Info<< "Writing field " << fieldName;
225  if (!equal(varScale, 1))
226  {
227  Info<< " (scaling " << varScale << ')';
228  }
229  Info<< " to " << outputFile << endl;
230  }
231 
232 
233  // Implicit geometry merge()
234  tmp<Field<Type>> tfield = mergeField(localValues) * varScale;
235 
236  const meshedSurf& surf = surface();
237 
238  if (Pstream::master() || !parallel_)
239  {
240  const auto& values = tfield();
241 
242  if (!isDir(outputFile.path()))
243  {
244  mkDir(outputFile.path());
245  }
246 
247  OFstream os(outputFile, streamOpt_);
248 
249  const labelUList& elemIds = surf.faceIds();
250 
251  // Possible to use faceIds?
252  const bool useOrigFaceIds =
253  (
254  elemIds.size() == values.size()
255  && !ListOps::found(elemIds, lessOp1<label>(0))
256  );
257 
258  label faceIndex = 0;
259 
260  // No header, just write values
261  for (const Type& val : values)
262  {
263  const label elemId =
264  (useOrigFaceIds ? elemIds[faceIndex] : faceIndex);
265 
266  os << (elemId + 1); // 1-based ids
267  writeData(os, val);
268  ++faceIndex;
269  }
270  }
271 
272  wroteGeom_ = true;
273  return outputFile;
274 }
275 
276 
277 // Field writing methods
279 
280 
281 // ************************************************************************* //
Foam::surfaceWriters::starcdWriter::write
virtual fileName write()
Definition: starcdSurfaceWriter.C:116
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:37
starcdSurfaceWriter.H
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::surfaceWriter
Base class for surface writers.
Definition: surfaceWriter.H:111
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
Foam::surfaceWriters::defineTypeName
defineTypeName(abaqusWriter)
Foam::fileName::path
static std::string path(const std::string &str)
Definition: fileNameI.H:169
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:57
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
Definition: HashOps.H:164
Foam::meshedSurf::faces
virtual const faceList & faces() const =0
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::meshedSurf
Abstract definition of a meshed surface defined by faces and points.
Definition: meshedSurf.H:43
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Definition: UPstream.H:453
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
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::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, abaqusWriter, word)
Foam::Field
Generic templated field type.
Definition: Field.H:59
Foam::Info
messageStream Info
Foam::meshedSurf::points
virtual const pointField & points() const =0
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::IOstreamOption::compressionEnum
static compressionType compressionEnum(const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
Definition: IOstreamOption.C:85
Foam::writeData
static void writeData(Ostream &os, const Type &val)
Definition: rawSurfaceWriterImpl.C:38
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:119
os
OBJstream os(runTime.globalPath()/outputName)
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::fileName::ext
word ext() const
Definition: fileNameI.H:211
Foam
Definition: atmBoundaryLayer.C:26
Foam::MeshedSurfaceProxy::write
static void write(const fileName &name, const MeshedSurfaceProxy &surf, IOstreamOption streamOpt=IOstreamOption(), const dictionary &options=dictionary::null)
Foam::dictionary::subOrEmptyDict
dictionary subOrEmptyDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX, const bool mandatory=false) const
Definition: dictionary.C:533
Foam::surfaceWriters::starcdWriter::starcdWriter
starcdWriter()
Definition: starcdSurfaceWriter.C:62
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:49
Foam::meshedSurf::faceIds
virtual const labelList & faceIds() const
Definition: meshedSurf.H:77
Foam::IOstreamOption::ASCII
@ ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:68
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:58
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:99
Foam::surfaceWriters::starcdWriter
A surfaceWriter for STARCD files.
Definition: starcdSurfaceWriter.H:105
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::lessOp1
Definition: ops.H:239
Foam::direction
uint8_t direction
Definition: direction.H:46
Foam::ListOps::found
bool found(const ListType &input, const UnaryPredicate &pred, const label start=0)
Definition: ListOpsTemplates.C:1149
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Definition: foamVtkOutputTemplates.C:29
ListOps.H
Various functions to operate on Lists.
Foam::UList::size
void size(const label n)
Definition: UList.H:110
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:52
Foam::UList::null
static const UList< T > & null()
Definition: UListI.H:46
defineSurfaceWriterWriteFields
defineSurfaceWriterWriteFields(Foam::surfaceWriters::starcdWriter)
Foam::labelUList
UList< label > labelUList
A UList of labels.
Definition: UList.H:81
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Definition: POSIX.C:533
Foam::equal
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:41
Foam::isDir
bool isDir(const fileName &name, const bool followLink=true)
Definition: POSIX.C:775