nastranSurfaceWriter.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 | Copyright (C) 2012-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "nastranSurfaceWriter.H"
27 #include "IOmanip.H"
28 #include "Tuple2.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  makeSurfaceWriterType(nastranSurfaceWriter);
36  addToRunTimeSelectionTable(surfaceWriter, nastranSurfaceWriter, wordDict);
37 
38  // Create write methods
39  defineSurfaceWriterWriteFields(nastranSurfaceWriter);
40 
41  template<>
43  {
44  "short",
45  "long",
46  "free"
47  };
48 
49 
52 
53  template<>
55  {
56  "PLOAD2",
57  "PLOAD4"
58  };
59 
62 }
63 
64 
65 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
66 
68 {
70 
71  // Capitalise the E marker
72  os.setf(ios_base::uppercase);
73 
74  label prec = 0;
75  label offset = 7;
76  switch (writeFormat_)
77  {
78  case (wfShort):
79  {
80  prec = 8 - offset;
81  break;
82  }
83  case (wfFree):
84  case (wfLong):
85  {
86  prec = 16 - offset;
87  break;
88  }
89  default:
90  {
91  }
92  }
93 
94  os.precision(prec);
95 }
96 
97 
99 (
100  const word& keyword,
101  Ostream& os
102 ) const
103 {
104  os.setf(ios_base::left);
105 
106  switch (writeFormat_)
107  {
108  case wfShort:
109  {
110  os << setw(8) << keyword;
111  break;
112  }
113  case wfLong:
114  {
115  os << setw(8) << word(keyword + '*');
116  break;
117  }
118  case wfFree:
119  {
120  os << keyword;
121  break;
122  }
123  }
124 
125  os.unsetf(ios_base::left);
126 }
127 
128 
130 (
131  const point& p,
132  const label pointI,
133  OFstream& os
134 ) const
135 {
136  // Fixed short/long formats:
137  // 1 GRID
138  // 2 ID : point ID - requires starting index of 1
139  // 3 CP : co-ordinate system ID (blank)
140  // 4 X1 : point x cp-ordinate
141  // 5 X2 : point x cp-ordinate
142  // 6 X3 : point x cp-ordinate
143  // 7 CD : co-ordinate system for displacements (blank)
144  // 8 PS : single point constraints (blank)
145  // 9 SEID : super-element ID
146 
147 
148  writeKeyword("GRID", os);
149 
150  os << separator_;
151 
152  os.setf(ios_base::right);
153 
154  writeValue(pointI + 1, os);
155  os << separator_;
156  writeValue("", os);
157  os << separator_;
158  writeValue(p.x(), os);
159  os << separator_;
160  writeValue(p.y(), os);
161  os << separator_;
162 
163  switch (writeFormat_)
164  {
165  case wfShort:
166  {
167  os << setw(8) << p.z()
168  << nl;
169  os.unsetf(ios_base::right);
170 
171  break;
172  }
173  case wfLong:
174  {
175  os << nl;
176  os.unsetf(ios_base::right);
177  writeKeyword("", os);
178  os.setf(ios_base::right);
179  writeValue(p.z(), os);
180  os << nl;
181 
182  break;
183  }
184  case wfFree:
185  {
186  writeValue(p.z(), os);
187  os << nl;
188 
189  break;
190  }
191  default:
192  {
194  << "Unknown writeFormat enumeration" << abort(FatalError);
195  }
196  }
197 
198  os.unsetf(ios_base::right);
199 }
200 
201 
203 (
204  const word& faceType,
205  const labelList& facePts,
206  label& nFace,
207  OFstream& os
208 ) const
209 {
210  // Only valid surface elements are CTRIA3 and CQUAD4
211 
212  // Fixed short/long formats:
213  // 1 CQUAD4
214  // 2 EID : element ID
215  // 3 PID : property element ID; default = EID (blank)
216  // 4 G1 : grid point index - requires starting index of 1
217  // 5 G2 : grid point index
218  // 6 G3 : grid point index
219  // 7 G4 : grid point index
220  // 8 onwards - not used
221 
222  // For CTRIA3 elements, cols 7 onwards are not used
223 
224  label PID = 1;
225 
226  writeKeyword(faceType, os);
227 
228  os << separator_;
229 
230  os.setf(ios_base::right);
231 
232  writeValue(nFace++, os);
233 
234  os << separator_;
235 
236  writeValue(PID, os);
237 
238  switch (writeFormat_)
239  {
240  case wfShort:
241  {
242  forAll(facePts, i)
243  {
244  writeValue(facePts[i] + 1, os);
245  }
246 
247  break;
248  }
249  case wfLong:
250  {
251  forAll(facePts, i)
252  {
253  writeValue(facePts[i] + 1, os);
254  if (i == 1)
255  {
256  os << nl;
257  os.unsetf(ios_base::right);
258  writeKeyword("", os);
259  os.setf(ios_base::right);
260  }
261  }
262 
263  break;
264  }
265  case wfFree:
266  {
267  forAll(facePts, i)
268  {
269  os << separator_;
270  writeValue(facePts[i] + 1, os);
271  }
272 
273  break;
274  }
275  default:
276  {
278  << "Unknown writeFormat enumeration" << abort(FatalError);
279  }
280  }
281 
282  os << nl;
283  os.unsetf(ios_base::right);
284 }
285 
286 
288 (
289  const pointField& points,
290  const faceList& faces,
291  List<DynamicList<face> >& decomposedFaces,
292  OFstream& os
293 ) const
294 {
295  // Write points
296 
297  os << "$" << nl
298  << "$ Points" << nl
299  << "$" << nl;
300 
301  forAll(points, pointI)
302  {
303  writeCoord(points[pointI], pointI, os);
304  }
305 
306 
307  // Write faces
308 
309  os << "$" << nl
310  << "$ Faces" << nl
311  << "$" << nl;
312 
313  label nFace = 1;
314 
315  forAll(faces, faceI)
316  {
317  const face& f = faces[faceI];
318 
319  if (f.size() == 3)
320  {
321  writeFace("CTRIA3", faces[faceI], nFace, os);
322  decomposedFaces[faceI].append(faces[faceI]);
323  }
324  else if (f.size() == 4)
325  {
326  writeFace("CQUAD4", faces[faceI], nFace, os);
327  decomposedFaces[faceI].append(faces[faceI]);
328  }
329  else
330  {
331  // Decompose poly face into tris
332  label nTri = 0;
333  faceList triFaces;
334  f.triangles(points, nTri, triFaces);
335 
336  forAll(triFaces, triI)
337  {
338  writeFace("CTRIA3", triFaces[triI], nFace, os);
339  decomposedFaces[faceI].append(triFaces[triI]);
340  }
341  }
342  }
343 }
344 
345 
347 {
348  label PID = 1;
349 
350  writeKeyword("PSHELL", os);
351 
352  os << separator_;
353 
354  writeValue(PID, os);
355 
356  for (label i = 0; i < 7; i++)
357  {
358  // Dummy values
359  os << separator_;
360  writeValue(1, os);
361  }
362 
363  os << nl;
364  writeKeyword("MAT1", os);
365  os << separator_;
366 
367  label MID = 1;
368 
369  writeValue(MID, os);
370 
371  for (label i = 0; i < 7; i++)
372  {
373  // Dummy values
374  os << separator_;
375  writeValue("", os);
376  }
377 
378  os << nl;
379 }
380 
381 
382 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
383 
385 :
386  surfaceWriter(),
387  writeFormat_(wfShort),
388  fieldMap_(),
389  scale_(1.0)
390 {}
391 
392 
394 :
395  surfaceWriter(),
396  writeFormat_(wfLong),
397  fieldMap_(),
398  scale_(options.lookupOrDefault("scale", 1.0)),
399  separator_("")
400 {
401  if (options.found("format"))
402  {
403  writeFormat_ = writeFormatNames_.read(options.lookup("format"));
404  }
405 
406  if (writeFormat_ == wfFree)
407  {
408  separator_ = ",";
409  }
410 
411  List<Tuple2<word, word> > fieldSet(options.lookup("fields"));
412 
413  forAll(fieldSet, i)
414  {
415  dataFormat format = dataFormatNames_[fieldSet[i].second()];
416 
417  fieldMap_.insert(fieldSet[i].first(), format);
418  }
419 }
420 
421 
422 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
423 
425 {}
426 
427 
428 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
429 
431 (
432  const fileName& outputDir,
433  const fileName& surfaceName,
434  const pointField& points,
435  const faceList& faces,
436  const bool verbose
437 ) const
438 {
439  if (!isDir(outputDir))
440  {
441  mkDir(outputDir);
442  }
443 
444  OFstream os(outputDir/surfaceName + ".nas");
445  formatOS(os);
446 
447  if (verbose)
448  {
449  Info<< "Writing nastran file to " << os.name() << endl;
450  }
451 
452  os << "TITLE=OpenFOAM " << surfaceName.c_str() << " mesh" << nl
453  << "$" << nl
454  << "BEGIN BULK" << nl;
455 
456  List<DynamicList<face> > decomposedFaces(faces.size());
457 
458  writeGeometry(points, faces, decomposedFaces, os);
459 
460  if (!isDir(outputDir))
461  {
462  mkDir(outputDir);
463  }
464 
465  writeFooter(os);
466 
467  os << "ENDDATA" << endl;
468 
469  return os.name();
470 }
471 
472 
473 // ************************************************************************* //
Foam::nastranSurfaceWriter::nastranSurfaceWriter
nastranSurfaceWriter()
Construct null.
Definition: nastranSurfaceWriter.C:384
format
word format(conversionProperties.lookup("format"))
p
p
Definition: pEqn.H:62
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::surfaceWriter
Base class for surface writers.
Definition: surfaceWriter.H:54
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::nastranSurfaceWriter::dataFormat
dataFormat
Definition: nastranSurfaceWriter.H:84
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Tuple2.H
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
Foam::nastranSurfaceWriter::~nastranSurfaceWriter
virtual ~nastranSurfaceWriter()
Destructor.
Definition: nastranSurfaceWriter.C:424
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::nastranSurfaceWriter::writeFace
void writeFace(const word &faceType, const labelList &facePts, label &nFace, OFstream &os) const
Write a face element (CTRIA3 or CQUAD4)
Definition: nastranSurfaceWriter.C:203
Foam::nastranSurfaceWriter::writeCoord
void writeCoord(const point &p, const label pointI, OFstream &os) const
Write a co-ordinate.
Definition: nastranSurfaceWriter.C:130
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::nastranSurfaceWriter::dataFormatNames_
static const NamedEnum< dataFormat, 2 > dataFormatNames_
Definition: nastranSurfaceWriter.H:90
Foam::nastranSurfaceWriter::separator_
word separator_
Separator used for free format.
Definition: nastranSurfaceWriter.H:107
nastranSurfaceWriter.H
makeSurfaceWriterMethods.H
Convenience macros for instantiating writer methods for surfaceWriter classes.
Foam::nastranSurfaceWriter::wfLong
@ wfLong
Definition: nastranSurfaceWriter.H:78
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:59
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::nastranSurfaceWriter::writeFooter
void writeFooter(Ostream &os) const
Write the footer information.
Definition: nastranSurfaceWriter.C:346
Foam::nastranSurfaceWriter::writeFormat_
writeFormat writeFormat_
Write option.
Definition: nastranSurfaceWriter.H:98
Foam::defineSurfaceWriterWriteFields
defineSurfaceWriterWriteFields(nastranSurfaceWriter)
IOmanip.H
Istream and Ostream manipulators taking arguments.
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::nastranSurfaceWriter::wfShort
@ wfShort
Definition: nastranSurfaceWriter.H:77
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::isDir
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:615
Foam::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:582
Foam::IOstream::unsetf
void unsetf(const ios_base::fmtflags uf)
Unset flags of stream.
Definition: IOstream.H:512
Foam::nastranSurfaceWriter::formatOS
void formatOS(OFstream &os) const
Initialise the output stream format params.
Definition: nastranSurfaceWriter.C:67
Foam::OFstream
Output to file stream.
Definition: OFstream.H:81
Foam::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:496
Foam::OSstream::precision
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:290
Foam::nastranSurfaceWriter::writeKeyword
void writeKeyword(const word &keyword, Ostream &os) const
Write the formatted keyword to the output stream.
Definition: nastranSurfaceWriter.C:99
Foam::NamedEnum::read
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
Foam::nastranSurfaceWriter::write
virtual fileName write(const fileName &outputDir, const fileName &surfaceName, const pointField &points, const faceList &faces, const bool verbose=false) const
Write single surface geometry to file.
Definition: nastranSurfaceWriter.C:431
Foam::OFstream::name
const fileName & name() const
Return the name of the stream.
Definition: OFstream.H:118
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::makeSurfaceWriterType
makeSurfaceWriterType(boundaryDataSurfaceWriter)
Foam::nastranSurfaceWriter::writeFormatNames_
static const NamedEnum< writeFormat, 3 > writeFormatNames_
Definition: nastranSurfaceWriter.H:82
Foam::mkDir
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:419
Foam::nastranSurfaceWriter::writeGeometry
void writeGeometry(const pointField &points, const faceList &faces, List< DynamicList< face > > &decomposedFaces, OFstream &os) const
Main driver to write the surface mesh geometry.
Definition: nastranSurfaceWriter.C:288
Foam::NamedEnum
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
Foam::nastranSurfaceWriter::fieldMap_
HashTable< dataFormat, word > fieldMap_
Mapping from field name to data format enumeration.
Definition: nastranSurfaceWriter.H:101
Foam::nastranSurfaceWriter::wfFree
@ wfFree
Definition: nastranSurfaceWriter.H:79