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-2016 OpenFOAM Foundation
6  \\/ M anipulation |
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>
33 void Foam::nastranSurfaceWriter::writeFaceValue
34 (
35  const word& nasFieldName,
36  const Type& value,
37  const label EID,
38  OFstream& os
39 ) const
40 {
41  // Fixed short/long formats:
42  // 1 Nastran distributed load type, e.g. PLOAD4
43  // 2 SID : load set ID
44  // 3 EID : element ID
45  // 4 onwards: load values
46 
47  label SID = 1;
48 
49  Type scaledValue = scale_*value;
50 
51  switch (writeFormat_)
52  {
53  case wfShort:
54  {
55  os.setf(ios_base::left);
56  os << setw(8) << nasFieldName;
57  os.unsetf(ios_base::left);
58  os.setf(ios_base::right);
59  os << setw(8) << SID
60  << setw(8) << EID;
61 
62  for (direction dirI = 0; dirI < pTraits<Type>::nComponents; dirI++)
63  {
64  os << setw(8) << component(scaledValue, dirI);
65  }
66 
67  os.unsetf(ios_base::right);
68 
69  break;
70  }
71  case wfLong:
72  {
73  os.setf(ios_base::left);
74  os << setw(8) << word(nasFieldName + "*");
75  os.unsetf(ios_base::left);
76  os.setf(ios_base::right);
77  os << setw(16) << SID
78  << setw(16) << EID;
79 
80  for (direction dirI = 0; dirI < pTraits<Type>::nComponents; dirI++)
81  {
82  os << setw(16) << component(scaledValue, dirI);
83  }
84 
85  os.unsetf(ios_base::right);
86 
87  os << nl;
88 
89  os.setf(ios_base::left);
90  os << '*';
91  os.unsetf(ios_base::left);
92 
93  break;
94  }
95  case wfFree:
96  {
97  os << nasFieldName << ','
98  << SID << ','
99  << EID;
100 
101  for (direction dirI = 0; dirI < pTraits<Type>::nComponents; dirI++)
102  {
103  os << ',' << component(scaledValue, dirI);
104  }
105 
106  break;
107  }
108  default:
109  {
110  }
111  }
112 
113  os << nl;
114 }
115 
116 
117 template<class Type>
118 void Foam::nastranSurfaceWriter::writeTemplate
119 (
120  const fileName& outputDir,
121  const fileName& surfaceName,
122  const pointField& points,
123  const faceList& faces,
124  const word& fieldName,
125  const Field<Type>& values,
126  const bool isNodeValues,
127  const bool verbose
128 ) const
129 {
130  if (!fieldMap_.found(fieldName))
131  {
133  << "No mapping found between field " << fieldName
134  << " and corresponding Nastran field. Available types are:"
135  << fieldMap_
136  << exit(FatalError);
137 
138  return;
139  }
140 
141  const word& nasFieldName(fieldMap_[fieldName]);
142 
143  if (!isDir(outputDir/fieldName))
144  {
145  mkDir(outputDir/fieldName);
146  }
147 
148  // const scalar timeValue = Foam::name(this->mesh().time().timeValue());
149  const scalar timeValue = 0.0;
150 
151  OFstream os(outputDir/fieldName/surfaceName + ".dat");
152  formatOS(os);
153 
154  if (verbose)
155  {
156  Info<< "Writing nastran file to " << os.name() << endl;
157  }
158 
159  os << "TITLE=OpenFOAM " << surfaceName.c_str() << " " << fieldName
160  << " data" << nl
161  << "$" << nl
162  << "TIME " << timeValue << nl
163  << "$" << nl
164  << "BEGIN BULK" << nl;
165 
166  List<DynamicList<face>> decomposedFaces(faces.size());
167 
168  writeGeometry(points, faces, decomposedFaces, os);
169 
170 
171  os << "$" << nl
172  << "$ Field data" << nl
173  << "$" << nl;
174 
175  if (isNodeValues)
176  {
177  label n = 0;
178 
179  forAll(decomposedFaces, i)
180  {
181  const DynamicList<face>& dFaces = decomposedFaces[i];
182  forAll(dFaces, facei)
183  {
184  Type v = Zero;
185  const face& f = dFaces[facei];
186 
187  forAll(f, fptI)
188  {
189  v += values[f[fptI]];
190  }
191  v /= f.size();
192 
193  writeFaceValue(nasFieldName, v, ++n, os);
194  }
195  }
196  }
197  else
198  {
199  label n = 0;
200 
201  forAll(decomposedFaces, i)
202  {
203  const DynamicList<face>& dFaces = decomposedFaces[i];
204 
205  forAll(dFaces, facei)
206  {
207  writeFaceValue(nasFieldName, values[facei], ++n, os);
208  }
209  }
210  }
211 
212  os << "ENDDATA" << endl;
213 }
214 
215 
216 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
uint8_t direction
Definition: direction.H:45
List< face > faceList
Definition: faceListFwd.H:43
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:536
static const zero Zero
Definition: zero.H:91
Istream and Ostream manipulators taking arguments.
static const char nl
Definition: Ostream.H:262
labelList f(nPoints)
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:297
#define WarningInFunction
Report a warning using Foam::Warning.
messageStream Info
label n
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)