nastranCoordSetWriter.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) 2018-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
29#include "coordSet.H"
30#include "IOmanip.H"
31#include "OFstream.H"
32#include "OSspecific.H"
35
36// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37
38namespace Foam
39{
40namespace coordSetWriters
41{
45}
46}
47
48
49// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
50
51namespace Foam
52{
53
54template<class Type>
55static inline void putValue(Ostream& os, const Type& value, const int width)
56{
57 if (width) os << setw(width);
58 os << value;
59}
60
61} // End namespace Foam
62
63
64// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
65
66Foam::Ostream& Foam::coordSetWriters::nastranWriter::writeKeyword
67(
68 Ostream& os,
69 const word& keyword
70) const
71{
72 return fileFormats::NASCore::writeKeyword(os, keyword, writeFormat_);
73}
74
75
76// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
77
79:
81 writeFormat_(fieldFormat::FREE),
82 separator_()
83{
84 if (writeFormat_ == fieldFormat::FREE)
85 {
86 separator_ = ",";
87 }
88}
89
90
92:
93 coordSetWriter(options),
94 writeFormat_
95 (
96 fileFormats::NASCore::fieldFormatNames.getOrDefault
97 (
98 "format",
99 options,
100 fieldFormat::FREE
101 )
102 ),
103 separator_()
104{
105 if (writeFormat_ == fieldFormat::FREE)
106 {
107 separator_ = ",";
108 }
109}
110
111
113(
114 const coordSet& coords,
115 const fileName& outputPath,
116 const dictionary& options
117)
118:
119 nastranWriter(options)
120{
121 open(coords, outputPath);
122}
123
124
126(
127 const UPtrList<coordSet>& tracks,
128 const fileName& outputPath,
129 const dictionary& options
130)
131:
132 nastranWriter(options)
133{
134 open(tracks, outputPath);
135}
136
137
138// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
139
141{
142 close();
143}
144
145
146// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
147
149{
150 // 1) rootdir/<TIME>/setName.{nas}
151 // 2) rootdir/setName.{nas}
152
153 return getExpectedPath("nas");
154}
155
156
158(
159 Ostream& os,
160 label nTracks
161) const
162{
163 if (coords_.empty())
164 {
165 return;
166 }
167
168 // Field width (SHORT, LONG formats)
169 const int width =
170 (
171 writeFormat_ == fieldFormat::SHORT ? 8
172 : writeFormat_ == fieldFormat::LONG ? 16
173 : 0
174 );
175
176 // Separator char (FREE format)
177 const char sep = (writeFormat_ == fieldFormat::FREE ? ',' : '\0');
178
179 // Write points
180 os << '$' << nl
181 << "$ Points" << nl
182 << '$' << nl;
183
184 label globalPointi = 0;
185 for (const coordSet& coords : coords_)
186 {
187 for (const point& p : coords)
188 {
190 (
191 os, p, globalPointi, writeFormat_
192 );
193 ++globalPointi;
194 }
195 }
196
197 if (nTracks)
198 {
199 // Write ids of track points to file
200 globalPointi = 0;
201 label globalEdgei = 0;
202
203 for (label tracki = 0; tracki < nTracks; ++tracki)
204 {
205 const label nEdges = (coords_[tracki].size() - 1);
206
207 for (label edgei = 0; edgei < nEdges; ++edgei)
208 {
209 writeKeyword(os, "PLOTEL");
210 if (sep) os << sep;
211
212 putValue(os, globalEdgei+1, width); // Edge id
213 if (sep) os << sep;
214
215 putValue(os, globalPointi+1, width);
216 if (sep) os << sep;
217
218 putValue(os, globalPointi+2, width);
219 os << nl;
220
221 ++globalEdgei;
222 ++globalPointi;
223 }
224 }
225 }
226
227 wroteGeom_ = true;
228}
229
230
231template<class Type>
232Foam::fileName Foam::coordSetWriters::nastranWriter::writeTemplate
233(
234 const word& fieldName,
235 const Field<Type>& values
236)
237{
238 checkOpen();
239 if (coords_.empty())
240 {
241 return fileName::null;
242 }
243
244 fileName outputFile = path();
245
246 if (!wroteGeom_)
247 {
248 if (verbose_)
249 {
250 Info<< "Writing nastran geometry to " << outputFile << endl;
251 }
252
253 if (!isDir(outputFile.path()))
254 {
255 mkDir(outputFile.path());
256 }
257
258 OFstream os(outputFile);
260
261 os << "TITLE=OpenFOAM " << outputFile.nameLessExt()
262 << " geometry" << nl
263 << "BEGIN BULK" << nl;
264
265 writeGeometry(os, (useTracks_ ? coords_.size() : 0));
266
267 os << "ENDDATA" << nl;
268 }
269
270 return outputFile;
271}
272
273
274template<class Type>
275Foam::fileName Foam::coordSetWriters::nastranWriter::writeTemplate
276(
277 const word& fieldName,
278 const List<Field<Type>>& fieldValues
279)
280{
281 checkOpen();
282 if (coords_.empty())
283 {
284 return fileName::null;
285 }
286
287 fileName outputFile = path();
288
289 if (!wroteGeom_)
290 {
291 if (verbose_)
292 {
293 Info<< "Writing nastran geometry to " << outputFile << endl;
294 }
295
296 if (!isDir(outputFile.path()))
297 {
298 mkDir(outputFile.path());
299 }
300
301 OFstream os(outputFile);
303
304 os << "TITLE=OpenFOAM " << outputFile.nameLessExt()
305 << " geometry" << nl
306 << "BEGIN BULK" << nl;
307
308 writeGeometry(os, coords_.size());
309
310 os << "ENDDATA" << nl;
311 }
312
313 return outputFile;
314}
315
316
317// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
318
319// Field writing methods
321
322
323// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
writer writeGeometry()
Generic templated field type.
Definition: Field.H:82
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
Output to file stream, using an OSstream.
Definition: OFstream.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
void writeGeometry()
Write the mesh.
Base class for writing coordSet(s) and tracks with fields.
virtual void open(const fileName &outputPath)
Write separate geometry to file.
Write coordSet(s) as Nastran plot lines. Does not do field data.
virtual fileName path() const
Characteristic output file name - information only.
virtual ~nastranWriter()
Destructor. Calls close()
Holds list of sampling positions.
Definition: coordSet.H:56
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
static Ostream & writeKeyword(Ostream &os, const word &keyword, const fieldFormat format)
Definition: NASCore.C:180
static void writeCoord(Ostream &os, const point &p, const label pointId, const fieldFormat format)
Write a GRID point.
Definition: NASCore.C:214
static void setPrecision(Ostream &os, const fieldFormat format)
Set output stream precision and format flags.
Definition: NASCore.C:146
fieldFormat
File field formats.
Definition: NASCore.H:64
A class for handling file names.
Definition: fileName.H:76
static std::string nameLessExt(const std::string &str)
Return basename, without extension.
Definition: fileName.C:396
static const fileName null
An empty fileName.
Definition: fileName.H:102
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeName(Type)
Define the typeName.
Definition: className.H:96
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
volScalarField & p
Convenience macros for instantiating coordSetWriter methods.
#define defineCoordSetWriterWriteFields(ThisClass)
OBJstream os(runTime.globalPath()/outputName)
const labelList nEdges(UPstream::listGatherValues< label >(aMesh.nEdges()))
Namespace for OpenFOAM.
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:515
messageStream Info
Information stream (stdout output on master, null elsewhere)
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
static void putValue(Ostream &os, const Type &value, const int width)
Definition: NASCore.C:64
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:651
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53