nastranSurfaceWriterTemplates.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 "OFstream.H"
27 #include "IOmanip.H"
28 #include "OSspecific.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 template<class Type>
34 (
35  const Type& value,
36  Ostream& os
37 ) const
38 {
39  switch (writeFormat_)
40  {
41  case wfShort:
42  {
43  os << setw(8) << value;
44  break;
45  }
46  case wfLong:
47  {
48  os << setw(16) << value;
49  break;
50  }
51  case wfFree:
52  {
53  os << value;
54  break;
55  }
56  }
57 }
58 
59 
60 template<class Type>
62 (
63  const dataFormat& format,
64  const Type& value,
65  const label EID,
66  Ostream& os
67 ) const
68 {
69  // Fixed short/long formats supporting PLOAD2 and PLOAD4:
70 
71  // PLOAD2:
72  // 1 descriptor : PLOAD2
73  // 2 SID : load set ID
74  // 3 data value : load value - MUST be singular
75  // 4 EID : element ID
76 
77  // PLOAD4:
78  // 1 descriptor : PLOAD4
79  // 2 SID : load set ID
80  // 3 EID : element ID
81  // 4 onwards : load values
82 
83  label SID = 1;
84 
85  Type scaledValue = scale_*value;
86 
87  // Write Keyword
88  writeKeyword(dataFormatNames_[format], os);
89 
90  os << separator_;
91 
92  // Write load set ID
93  os.setf(ios_base::right);
94  writeValue(SID, os);
95 
96  os << separator_;
97 
98  switch (format)
99  {
100  case dfPLOAD2:
101  {
103  {
104  writeValue(scaledValue, os);
105  }
106  else
107  {
109  << dataFormatNames_[format] << " requires scalar values "
110  << "and cannot be used for higher rank values"
111  << endl;
112 
113  writeValue(scalar(0), os);
114  }
115 
116  os << separator_;
117  writeValue(EID, os);
118 
119  break;
120  }
121  case dfPLOAD4:
122  {
123  writeValue(EID, os);
124 
125  for (direction dirI = 0; dirI < pTraits<Type>::nComponents; dirI++)
126  {
127  os << separator_;
128  writeValue(component(scaledValue, dirI), os);
129  }
130  break;
131  }
132  default:
133  {
135  << "Unhandled enumeration " << dataFormatNames_[format]
136  << exit(FatalError);
137  }
138  }
139 
140  os.unsetf(ios_base::right);
141 
142  os << nl;
143 }
144 
145 
146 template<class Type>
148 (
149  const fileName& outputDir,
150  const fileName& surfaceName,
151  const pointField& points,
152  const faceList& faces,
153  const word& fieldName,
154  const Field<Type>& values,
155  const bool isNodeValues,
156  const bool verbose
157 ) const
158 {
159  if (!fieldMap_.found(fieldName))
160  {
162  << "No mapping found between field " << fieldName
163  << " and corresponding Nastran field. Available types are:"
164  << fieldMap_
165  << exit(FatalError);
166 
167  return fileName::null;
168  }
169 
170  const dataFormat& format(fieldMap_[fieldName]);
171 
172  if (!isDir(outputDir/fieldName))
173  {
174  mkDir(outputDir/fieldName);
175  }
176 
177  // const scalar timeValue = Foam::name(this->mesh().time().timeValue());
178  const scalar timeValue = 0.0;
179 
180  OFstream os(outputDir/fieldName/surfaceName + ".nas");
181  formatOS(os);
182 
183  if (verbose)
184  {
185  Info<< "Writing nastran file to " << os.name() << endl;
186  }
187 
188  os << "TITLE=OpenFOAM " << surfaceName.c_str() << " " << fieldName
189  << " data" << nl
190  << "$" << nl
191  << "TIME " << timeValue << nl
192  << "$" << nl
193  << "BEGIN BULK" << nl;
194 
195  List<DynamicList<face> > decomposedFaces(faces.size());
196 
197  writeGeometry(points, faces, decomposedFaces, os);
198 
199 
200  os << "$" << nl
201  << "$ Field data" << nl
202  << "$" << nl;
203 
204  if (isNodeValues)
205  {
206  label n = 0;
207 
208  forAll(decomposedFaces, i)
209  {
210  const DynamicList<face>& dFaces = decomposedFaces[i];
211  forAll(dFaces, faceI)
212  {
213  Type v = pTraits<Type>::zero;
214  const face& f = dFaces[faceI];
215 
216  forAll(f, fptI)
217  {
218  v += values[f[fptI]];
219  }
220  v /= f.size();
221 
222  writeFaceValue(format, v, ++n, os);
223  }
224  }
225  }
226  else
227  {
228  label n = 0;
229 
230  forAll(decomposedFaces, i)
231  {
232  const DynamicList<face>& dFaces = decomposedFaces[i];
233 
234  forAll(dFaces, faceI)
235  {
236  writeFaceValue(format, values[faceI], ++n, os);
237  }
238  }
239  }
240 
241  writeFooter(os);
242 
243  os << "ENDDATA" << endl;
244 
245  return os.name();
246 }
247 
248 
249 // ************************************************************************* //
format
word format(conversionProperties.lookup("format"))
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::nastranSurfaceWriter::writeTemplate
fileName writeTemplate(const fileName &outputDir, const fileName &surfaceName, const pointField &points, const faceList &faces, const word &fieldName, const Field< Type > &values, const bool isNodeValues, const bool verbose) const
Templated write operation.
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:41
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
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
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::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
OFstream.H
n
label n
Definition: TABSMDCalcMethod2.H:31
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::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
IOmanip.H
Istream and Ostream manipulators taking arguments.
Foam::FatalError
error FatalError
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::IOstream::unsetf
void unsetf(const ios_base::fmtflags uf)
Unset flags of stream.
Definition: IOstream.H:512
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
f
labelList f(nPoints)
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
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:50
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::direction
unsigned char direction
Definition: direction.H:43
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
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::nastranSurfaceWriter::writeValue
void writeValue(const Type &value, Ostream &os) const
Write a formatted value to the output stream.
Definition: nastranSurfaceWriterTemplates.C:34
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
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::nastranSurfaceWriter::writeFaceValue
void writeFaceValue(const dataFormat &format, const Type &value, const label EID, Ostream &os) const
Write a face-based value.
Definition: nastranSurfaceWriterTemplates.C:62