nastranSurfaceWriterTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2012-2020 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 (format_)
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::Write
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
128 {
129  if (!fieldMap_.found(fieldName))
130  {
132  << "No mapping found between field " << fieldName
133  << " and corresponding Nastran field. Available types are:"
134  << fieldMap_
135  << exit(FatalError);
136 
137  return;
138  }
139 
140  const word& nasFieldName(fieldMap_[fieldName]);
141 
142  if (!isDir(outputDir/fieldName))
143  {
144  mkDir(outputDir/fieldName);
145  }
146 
147  // const scalar timeValue = Foam::name(this->mesh().time().timeValue());
148  const scalar timeValue = 0.0;
149 
150  OFstream os(outputDir/fieldName/surfaceName + ".dat");
151  formatOS(os);
152 
153  if (debug)
154  {
155  Info<< "Writing nastran file to " << os.name() << endl;
156  }
157 
158  os << "TITLE=OpenFOAM " << surfaceName.c_str() << " " << fieldName
159  << " data" << nl
160  << "$" << nl
161  << "TIME " << timeValue << nl
162  << "$" << nl
163  << "BEGIN BULK" << nl;
164 
165  List<DynamicList<face>> decomposedFaces(faces.size());
166 
167  writeGeometry(points, faces, decomposedFaces, os);
168 
169 
170  os << "$" << nl
171  << "$ Field data" << nl
172  << "$" << nl;
173 
174  if (isNodeValues)
175  {
176  label n = 0;
177 
178  forAll(decomposedFaces, i)
179  {
180  const DynamicList<face>& dFaces = decomposedFaces[i];
181  forAll(dFaces, facei)
182  {
183  Type v = Zero;
184  const face& f = dFaces[facei];
185 
186  forAll(f, fptI)
187  {
188  v += values[f[fptI]];
189  }
190  v /= f.size();
191 
192  writeFaceValue(nasFieldName, v, ++n, os);
193  }
194  }
195  }
196  else
197  {
198  label n = 0;
199 
200  forAll(decomposedFaces, i)
201  {
202  const DynamicList<face>& dFaces = decomposedFaces[i];
203 
204  forAll(dFaces, facei)
205  {
206  writeFaceValue(nasFieldName, values[facei], ++n, os);
207  }
208  }
209  }
210 
211  os << "ENDDATA" << endl;
212 }
213 
214 
215 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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:251
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:539
static const zero Zero
Definition: zero.H:97
Istream and Ostream manipulators taking arguments.
static const char nl
Definition: Ostream.H:260
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:290
#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)