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 :
71  surfaceWriter(),
72  streamOpt_(),
73  fieldScale_()
74 {}
75 
76 
78 (
79  const dictionary& options
80 )
81 :
82  surfaceWriter(options),
83  streamOpt_
84  (
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 
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()
Write surface geometry to file.
Definition: starcdSurfaceWriter.C:123
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: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::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::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.
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:82
OFstream.H
Foam::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, abaqusWriter, word)
Foam::Field< vector >
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
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:45
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
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:218
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::surfaceWriters::starcdWriter::starcdWriter
starcdWriter()
Default construct.
Definition: starcdSurfaceWriter.C:69
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::meshedSurf::faceIds
virtual const labelList & faceIds() const
Per-face identifier (eg, element Id)
Definition: meshedSurf.H:83
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List< face >
Foam::UList< label >
Foam::surfaceWriters::starcdWriter
A surfaceWriter for STARCD files.
Definition: starcdSurfaceWriter.H:109
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::lessOp1
Definition: ops.H:245
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::ListOps::found
bool found(const ListType &input, const UnaryPredicate &pred, const label start=0)
True if there is a value in the list that satisfies the predicate.
Definition: ListOpsTemplates.C:1156
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
ListOps.H
Various functions to operate on Lists.
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
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:53
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::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