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 -------------------------------------------------------------------------------
10 License
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 
28 #include "nastranCoordSetWriter.H"
29 #include "coordSet.H"
30 #include "IOmanip.H"
31 #include "OFstream.H"
32 #include "OSspecific.H"
33 #include "coordSetWriterMethods.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 namespace coordSetWriters
41 {
42  defineTypeName(nastranWriter);
43  addToRunTimeSelectionTable(coordSetWriter, nastranWriter, word);
44  addToRunTimeSelectionTable(coordSetWriter, nastranWriter, wordDict);
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 template<class Type>
55 static 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 
66 Foam::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 
157 void Foam::coordSetWriters::nastranWriter::writeGeometry
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  }
227  wroteGeom_ = true;
228 }
229 
230 
231 template<class Type>
232 Foam::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);
259  fileFormats::NASCore::setPrecision(os, writeFormat_);
260 
261  os << "TITLE=OpenFOAM " << outputFile.stem()
262  << " geometry" << nl
263  << "BEGIN BULK" << nl;
264 
265  writeGeometry(os, (useTracks_ ? coords_.size() : 0));
266 
267  os << "ENDDATA" << nl;
268  }
270  return outputFile;
271 }
272 
273 
274 template<class Type>
275 Foam::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);
302  fileFormats::NASCore::setPrecision(os, writeFormat_);
303 
304  os << "TITLE=OpenFOAM " << outputFile.stem()
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 // ************************************************************************* //
addToRunTimeSelectionTable(coordSetWriter, abaqusWriter, word)
A class for handling file names.
Definition: fileName.H:72
writer writeGeometry()
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
static const fileName null
An empty fileName.
Definition: fileName.H:111
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
defineTypeName(abaqusWriter)
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:169
Write coordSet(s) as Nastran plot lines. Does not do field data.
defineCoordSetWriterWriteFields(Foam::coordSetWriters::nastranWriter)
static std::string stem(const std::string &str)
Return the basename, without extension.
Definition: fileName.C:391
Macros for easy insertion into run-time selection tables.
static void putValue(Ostream &os, const Type &value, const int width)
Definition: NASCore.C:71
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:860
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Convenience macros for instantiating coordSetWriter methods.
Holds list of sampling positions.
Definition: coordSet.H:49
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:614
static void setPrecision(Ostream &os, const fieldFormat format)
Set output stream precision and format flags.
Definition: NASCore.C:153
Generic templated field type.
Definition: Field.H:62
virtual ~nastranWriter()
Destructor. Calls close()
A class for handling words, derived from Foam::string.
Definition: word.H:63
Base class for writing coordSet(s) and tracks with fields.
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
static void writeCoord(Ostream &os, const point &p, const label pointId, const fieldFormat format)
Write a GRID point.
Definition: NASCore.C:221
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
Istream and Ostream manipulators taking arguments.
OBJstream os(runTime.globalPath()/outputName)
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
virtual fileName path() const
Characteristic output file name - information only.
vector point
Point is a vector.
Definition: point.H:37
messageStream Info
Information stream (stdout output on master, null elsewhere)
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
volScalarField & p
static Ostream & writeKeyword(Ostream &os, const word &keyword, const fieldFormat format)
Write initial keyword (eg, &#39;GRID&#39; or &#39;GRID*&#39;) followed by the requisite number of spaces for the fiel...
Definition: NASCore.C:187
Namespace for OpenFOAM.